Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 未分配正在重新分配的指针_C_Malloc_Realloc - Fatal编程技术网

C 未分配正在重新分配的指针

C 未分配正在重新分配的指针,c,malloc,realloc,C,Malloc,Realloc,我试图动态分配一个结构数组,但每当我运行程序时,我总是得到:a.out(6487,0x7fff7ecb8300)malloc:*对象0x7fff6f670000的错误:未分配realloc'd的指针 *在malloc\u error\u break中设置断点以进行调试 struct node { char course[25]; char category[20]; char prereq[50]; char notes[50]; }; int main(i

我试图动态分配一个结构数组,但每当我运行程序时,我总是得到:a.out(6487,0x7fff7ecb8300)malloc:*对象0x7fff6f670000的错误:未分配realloc'd的指针 *在malloc\u error\u break中设置断点以进行调试

struct node {
    char course[25];
    char category[20];
    char prereq[50];
    char notes[50];
};



int main(int argc, char* argv[])
{
    FILE *fp;
    char *filename = argv[1];
    char *token;

    char buffer[100];
    char *del = ",\n";
    int num = 5, i = 0, j =0, count = 0;
    struct node *d = malloc(num * sizeof(struct node));
    char** complete = malloc(num * sizeof(char*));
    printf("%s\n", filename);


    if( (fp = fopen(filename, "r")) == NULL )
    {
        printf("unable to open %s\n", filename);
        exit(1);
    }
    while(fgets(buffer, sizeof(buffer), fp) != NULL)
    {

        if(count == num)
        {
            num = num + 5;
            struct node *d = realloc(d, sizeof(d)*num);
            printf("Reallocating\n");
        }  
        token = strtok(buffer, del);

        if(strncmp(token, "#", 1) != 0)
        {   

            strcpy(d[count].course, token);
            printf("%s\n", d[count].course);
            strcpy(d[count].category, strtok(NULL, del));
            printf("%s\n", d[count].category);
            strcpy(d[count].prereq, strtok(NULL, del));
            printf("%s\n", d[count].prereq);
            strcpy(d[count].notes, strtok(NULL, del));
            printf("%s\n", d[count].notes);
            count++;
        }


    }
在:

它声明了一个新变量
d
,初始值未确定,并将其传递到
realloc
。改为:

struct node *tmp = realloc(d, sizeof(*d)*num);
if(!tmp)
    ; // handle error
d = tmp;
您正在声明一个新的
d
变量,该变量将前一个变量隐藏起来,并将其尚未初始化的值提供给
realloc

您需要这样做:

struct node *newD = realloc(d, num * sizeof *d);
if(!newD) {
    // Allocation failure, do something about it and break out
} /* else */
d = newD;

还请注意,我更正了
sizeof
,它测量的是指针的大小,而不是指针的大小。

在使用
argv[1]
之前,请检查
argc==2
。只是一个建议。警告:当在
malloc
中调用
sizeof
(以及类似内容)作为
ptr=malloc(sizeof(*ptr)*…)而不是
ptr=malloc(sizeof(ptrtype*)*…)struct node*d=realloc(d,sizeof(d)*num)
错误:这应该使用
sizeof(*b)
,而不是
sizeof(b)
这不分配
sizeof(任何指针)
?我希望有
sizeof(struct node)
。@Jongware我纠正了这一点,但没有提及,希望读者注意。谢谢!这一切都解决了!
struct node *d = realloc(d, sizeof(d)*num);
struct node *newD = realloc(d, num * sizeof *d);
if(!newD) {
    // Allocation failure, do something about it and break out
} /* else */
d = newD;