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

时间:2017-05-25 14:29:19

标签: c list stack-overflow dynamic-memory-allocation

我在学校项目上遇到了一些麻烦。

我需要做的很简单,我只需要在列表中添加一些数据。 我通常知道该怎么做,但内存分配是动态的这一事实让我很困惑。

所以这是列表:

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");
}

3 个答案:

答案 0 :(得分:1)

在第一个malloc之后,concept成员具有非正确的价值。

您需要为之前传递的字符串分配空间

strcpy(link->concept,concept);

所以你需要

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

sentence成员同样如此。

答案 1 :(得分:0)

您需要分配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");
}

答案 2 :(得分:0)

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

您需要做的是为这些指针分配空间,然后执行复制:

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

您也可以使用strdup

一步完成此操作
link->concept = strdup(concept);