将 char* 作为参数传递会阻止函数工作
Passing char* as an argument prevents function from working
我真的很抱歉我的问题措辞不当,但我真的不知道如何更准确。我真的不明白我遇到的问题。首先,这是我的代码
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
int gettimeofday();
void lance_commande(char *commande)
{
int res;
float t1,t2;
t1=gettimeofday();
res=system(commande);
t2=gettimeofday();
printf("Temps écoulé : %f secondes\n",t2-t1);
if(res==-1)
{
printf("Une erreur est survenue\n");
}
}
int main(int argc, char *argv[])
{
int i;
if (argc>1)
{
for(i=1;i<argc;i++)
{
lance_commande(argv[i]);
printf("\n");
}
}
else
{
printf("Aucun argument n'a été passé en entrée");
}
return 0;
}
我的程序所做的是使用函数 lance_commande 执行一些 shell 命令,当我从 shell 启动我的程序时,我要执行的命令作为参数传递,像那样:./program "command1" "command2" ...
当我执行我用参数粘贴的代码时: ./program "sleep 1" "sleep 2"
,它输出 Segmentation Fault (core dumped)
。我是 C 的初学者,所以我不太明白为什么会这样,我听说这是内存访问的一些问题,但我仍然不知道如何解决它。
在我的主函数中,在 for 循环中,当我调用 system() 函数而不是调用 lance_command
函数时,命令 运行 非常完美。
在我的 lance_command
函数中,当我调用 system(*commande)
时,我尝试使用 commande 而不是 *commande
作为参数,我收到的输出发生了变化:
sh: 1: Syntax error: EOF in backquote substitution
Segmentation fault (core dumped)
我不知道发生了什么,有人可以帮助我了解问题出在哪里吗?对不起,如果我的问题的答案已经存在,但我不知道我应该寻找什么样的问题。
编辑:我做了修改,删除了 system() 函数输出的关于 int 到 char* 转换的警告,并将 *commande
替换为 commande
作为系统函数的参数,同时仍然将 *commande
作为 lance_commande 函数的参数。
错误没有改变,./program "sleep 1" "sleep 2"
输出
sh: 1: Syntax error: EOF in backquote substitution
Segmentation fault (core dumped)
所以问题已经解决,如编辑中所述。感谢@Bodo 的帮助。
这是最终代码,使用 gettimeofday :
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/time.h>
void lance_commande(char *commande)
{
int res;
struct timeval t1,t2;
gettimeofday(&t1,NULL);
res=system(commande);
gettimeofday(&t2,NULL);
printf("Duration : %ld seconds et %ld microseconds\n",t2.tv_sec-t1.tv_sec,t2.tv_usec-t1.tv_usec);
if(res==-1)
{
printf("Error\n");
}
}
int main(int argc, char *argv[])
{
int i;
if (argc>1)
{
for(i=1;i<argc;i++)
{
lance_commande(argv[i]);
printf("\n");
}
}
else
{
printf("No input provided");
}
return 0;
}
在我的 lance_commande() 函数中为 system() 函数设置参数的正确方法最终是 system(commande)
。
代码崩溃的原因是因为 gettimeofday 声明。我错误地明确声明了它,导致了分段错误。我删除了声明并包含 sys/time.h,关于隐式声明的警告消失了。
阅读有关 gettimeofday (https://linuxhint.com/gettimeofday_c_language/) 的解释后,我设法理解了它的工作原理并打印了花费的秒数和微秒数。正如@Bodo 指出的那样,使用 t1=time(NULL) [之后的 t2 相同] 会是一个更好的解决方案,但使用 gettimeofday 对我来说是必须的。
我真的很抱歉我的问题措辞不当,但我真的不知道如何更准确。我真的不明白我遇到的问题。首先,这是我的代码
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
int gettimeofday();
void lance_commande(char *commande)
{
int res;
float t1,t2;
t1=gettimeofday();
res=system(commande);
t2=gettimeofday();
printf("Temps écoulé : %f secondes\n",t2-t1);
if(res==-1)
{
printf("Une erreur est survenue\n");
}
}
int main(int argc, char *argv[])
{
int i;
if (argc>1)
{
for(i=1;i<argc;i++)
{
lance_commande(argv[i]);
printf("\n");
}
}
else
{
printf("Aucun argument n'a été passé en entrée");
}
return 0;
}
我的程序所做的是使用函数 lance_commande 执行一些 shell 命令,当我从 shell 启动我的程序时,我要执行的命令作为参数传递,像那样:./program "command1" "command2" ...
当我执行我用参数粘贴的代码时: ./program "sleep 1" "sleep 2"
,它输出 Segmentation Fault (core dumped)
。我是 C 的初学者,所以我不太明白为什么会这样,我听说这是内存访问的一些问题,但我仍然不知道如何解决它。
在我的主函数中,在 for 循环中,当我调用 system() 函数而不是调用 lance_command
函数时,命令 运行 非常完美。
在我的 lance_command
函数中,当我调用 system(*commande)
时,我尝试使用 commande 而不是 *commande
作为参数,我收到的输出发生了变化:
sh: 1: Syntax error: EOF in backquote substitution
Segmentation fault (core dumped)
我不知道发生了什么,有人可以帮助我了解问题出在哪里吗?对不起,如果我的问题的答案已经存在,但我不知道我应该寻找什么样的问题。
编辑:我做了修改,删除了 system() 函数输出的关于 int 到 char* 转换的警告,并将 *commande
替换为 commande
作为系统函数的参数,同时仍然将 *commande
作为 lance_commande 函数的参数。
错误没有改变,./program "sleep 1" "sleep 2"
输出
sh: 1: Syntax error: EOF in backquote substitution
Segmentation fault (core dumped)
所以问题已经解决,如编辑中所述。感谢@Bodo 的帮助。 这是最终代码,使用 gettimeofday :
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/time.h>
void lance_commande(char *commande)
{
int res;
struct timeval t1,t2;
gettimeofday(&t1,NULL);
res=system(commande);
gettimeofday(&t2,NULL);
printf("Duration : %ld seconds et %ld microseconds\n",t2.tv_sec-t1.tv_sec,t2.tv_usec-t1.tv_usec);
if(res==-1)
{
printf("Error\n");
}
}
int main(int argc, char *argv[])
{
int i;
if (argc>1)
{
for(i=1;i<argc;i++)
{
lance_commande(argv[i]);
printf("\n");
}
}
else
{
printf("No input provided");
}
return 0;
}
在我的 lance_commande() 函数中为 system() 函数设置参数的正确方法最终是 system(commande)
。
代码崩溃的原因是因为 gettimeofday 声明。我错误地明确声明了它,导致了分段错误。我删除了声明并包含 sys/time.h,关于隐式声明的警告消失了。
阅读有关 gettimeofday (https://linuxhint.com/gettimeofday_c_language/) 的解释后,我设法理解了它的工作原理并打印了花费的秒数和微秒数。正如@Bodo 指出的那样,使用 t1=time(NULL) [之后的 t2 相同] 会是一个更好的解决方案,但使用 gettimeofday 对我来说是必须的。