添加动态变量时堆栈溢出 C 列表

Stack overflow with C list when adding dynamic variables

我在学校项目中遇到了一些问题。

我需要做的很简单,只需向列表中添加一些数据即可。 我通常知道如何这样做,但内存分配是动态的这一事实让我感到困惑。

列表如下:

typedef struct cList {
char *concept; // the concept learned
char *sentence; // the sentence associated with the concept
int timesUsed; //no of times the concept was used to an answer
char learnedFrom[5]; //learned either from "file" or "kbrd"
struct cList *next;
} conceptList;

以及将新数据添加到列表的函数:

void insert(char *concept, char *sentence, int timesUsed, char learnedFrom[5]) 
{
    int flag = 0, temp; 

    struct cList *link = (struct cList*) malloc(sizeof(struct cList));

    if(link!=NULL)
    {
        strcpy(link->concept,concept); //this is where the stack overflow happens first.
        strcpy(link->sentence,sentence);
        link->timesUsed = timesUsed;
        strcpy(link->learnedFrom,learnedFrom);

        link->next = head;
        head = link;

        temp = rand()%5;
        if(temp==0)
            printf("3B$ It sure is great to know everything about %s.\n", concept);
        else if(temp==1)
            printf("3B$ This information about %s is quite interesting.", concept);
        else if(temp==2)
            printf("3B$ I have to admit, learning about %s is much more interesting than it seems.", concept);
        else if(temp==3)
            printf("3B$ Learning about %s wasn't even a challenge.", concept);
        else if(temp==4)
            printf("3B$ Wow, learning about %s was so exciting!", concept);
    }
    else
        printf("3B$ Memory not available!!!\n");
}

在第一个 malloc 之后,concept 成员具有未初始化的值。

你需要在to

之前为传递的字符串分配space
strcpy(link->concept,concept);

所以你需要

link->concept = malloc(strlen(concept)+1);
if (link->concept != NULL)
{
    strcpy(link->concept,concept);
}
else
{
    free(link)
    return;
}

sentence 成员也一样。

您需要分配 link->conceptlink->sentence(请参阅第 7 行)。或者将它们声明为数组(char concept[100] 而不是 char *concept

重要提示:请始终使用 strncpy 而不是 strcpy

void insert(char *concept, char *sentence, int timesUsed, char learnedFrom[5]) 
{
    int flag = 0, temp; 

    struct cList *link = (struct cList*) malloc(sizeof(struct cList));

    // Add these:
    link->concept = malloc...
    link->sentence = malloc...

    if(link!=NULL)
    {
        strcpy(link->concept,concept); //this is where the stack overflow happens first.
        strcpy(link->sentence,sentence);
        link->timesUsed = timesUsed;
        strcpy(link->learnedFrom,learnedFrom);

        link->next = head;
        head = link;

        temp = rand()%5;
        if(temp==0)
            printf("3B$ It sure is great to know everything about %s.\n", concept);
        else if(temp==1)
            printf("3B$ This information about %s is quite interesting.", concept);
        else if(temp==2)
            printf("3B$ I have to admit, learning about %s is much more interesting than it seems.", concept);
        else if(temp==3)
            printf("3B$ Learning about %s wasn't even a challenge.", concept);
        else if(temp==4)
            printf("3B$ Wow, learning about %s was so exciting!", concept);
    }
    else
        printf("3B$ Memory not available!!!\n");
}

您分配的结构中的 conceptsentence 指针尚未初始化。所以当你使用 strcpy 时,你是在取消引用一个未初始化的指针。这会调用 undefined behavior,在这种情况下会导致程序崩溃。

你需要做的是为这些指针也分配space,然后执行复制:

link->concept = malloc(strlen(concept) + 1);
strcpy(link->concept,concept);

您也可以使用 strdup 一步完成:

link->concept = strdup(concept);