分段错误-自定义Shell #包括 #包括 #包括 #define BUF 1024//我假设最大参数数为1024 main() { 字符c; 字符*温度; char*arg[BUF];//命令 int i=1,j,k,iter=0; 而(1) { i=1; iter=0; printf(“CS21>”); temp=malloc(sizeof(char)); 而((c=fgetc(stdin))!='\n') { temp=realloc(temp,i*sizeof(char)); 温度[i-1]=c; i++; } j=0; 而(j

分段错误-自定义Shell #包括 #包括 #包括 #define BUF 1024//我假设最大参数数为1024 main() { 字符c; 字符*温度; char*arg[BUF];//命令 int i=1,j,k,iter=0; 而(1) { i=1; iter=0; printf(“CS21>”); temp=malloc(sizeof(char)); 而((c=fgetc(stdin))!='\n') { temp=realloc(temp,i*sizeof(char)); 温度[i-1]=c; i++; } j=0; 而(j,c,shell,segmentation-fault,C,Shell,Segmentation Fault,我认为以下几点: #include<stdio.h> #include<string.h> #include<stdlib.h> #define BUF 1024 //I assume that the maximum number of arguments is 1024 main() { char c; char *temp; char *arg[BUF]; //the comman

我认为以下几点:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

#define BUF 1024        //I assume that the maximum number of arguments is 1024

main()
{
    char c;
    char *temp;
    char *arg[BUF];                 //the commands
    int i=1,j,k,iter=0;

    while(1)
    {
            i=1;
            iter=0;
            printf("CS21> ");
            temp = malloc(sizeof(char));
            while((c=fgetc(stdin))!='\n')
            {
                    temp = realloc(temp, i*sizeof(char));

                    temp[i-1]=c;
                    i++;
            }

            j=0;
            while(j<strlen(temp))
            {
                    if(temp[j]==' ')
                    {
                            j++;
                            continue;
                    }

                    if(temp[j]!=' ')  //Line 38: Same check performed as Line 42
                    {
                                    k=j;
                                    arg[iter] = malloc(sizeof(char));
                                    while(temp[k]!=' ')    //Line 42: Segmentation Fault here
                                    {
                                            arg[iter] = realloc(arg[iter],(k-j+1)*sizeof(char));
                                            arg[iter][k-j]=temp[k];
                                            k++;
                                    }
                                    iter++;
                                    k++;
                                    j=k;
                                    continue;
                    }
            }
    }
}
如果
item
大于BUF,则会损坏
k
的值。如果
iter
为负值,则也会损坏
temp
的值。这是因为
k
temp
存储在堆栈上,稍微靠近
arg
的位置,写入
arg
元素超过其大小实际上可能会覆盖variab他们在附近储存了一批货物

尝试在上述行前后打印
k
temp
,查看它们的值是否损坏。

在行中

arg[iter] = malloc(sizeof(char));
临时字符串不能以空终止字符结尾

然后你就超出了这个数组的范围

while((c=fgetc(stdin))!='\n')
{
        temp = realloc(temp, i*sizeof(char));

        temp[i-1]=c;
        i++;
}



换线

 while(temp[k]!=' ')    //Line 42: Segmentation Fault here

当字符串中没有空格时,while(temp[k]!='')中的循环没有结束(最后一个参数的情况)。如果k>strlen(temp),则需要停止循环


只是我的评论:到底是谁在教你在每个字符后按字节和realloc读取?这很尴尬…

你需要为
temp
分配额外的一个字符空间,用于表示字符串结尾的特殊
'\0'
字符

while(temp[k]!=' ') to while(temp[k]!='\0')
并不是说
arg
字符串也必须以
'\0'
结尾

这将防止出现分段错误,但您可能需要检查程序可能出现故障的其他情况


有关更多信息,请参阅。

显然您正在访问超出数组界限的内容。请进行一些调试以检查索引值。提示:您不终止
temp
字符串。或
arg
字符串。我实际上使用gdb隔离该行。但您尚未回答以下问题-它如何接受一行和一个字符串然后在一条完全相似的直线上给出一个分段错误(逻辑相似)?@Karthik请看下面的答案这是一个非常糟糕的做法。您应该始终添加空终止字符!如果它从未看到“\n”并不断重新定位并分配给从最终的realloc返回的空指针,它可能会崩溃。谢谢您的V-X!您的建议奏效了!顺便问一句,您能告诉我一种更好的方法来读取命令并执行sam吗操作?
char*buffer;buffer=(char*)malloc(1024);fgets(buffer,sizeof(buffer),stdin);
如果字符串以换行符结尾,并且输入更长,则可以重新定位缓冲区并读取另一部分。
while(temp[k]!=' ') to while(temp[k]!='\0')
while((c=fgetc(stdin))!='\n')
{
    temp = realloc(temp, i*sizeof(char) + 1);  //1 more char space for '\0'
    temp[i-1]=c;
    i++;
 }
 temp[i] = '\0';  //Indicates the end of String