【问题标题】:strcmp segmentation fault with array of structs结构数组的strcmp分段错误
【发布时间】:2013-04-17 06:09:34
【问题描述】:

所以我有这段代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "hw09-header.h"

struct student
{
    char* name;
    char* course;
};

int main(int argc, char* argv[])
{
    int i = 0, init_size=10,x,z;
    char *value = "go";
    int key, count=0;
    char* del = ","; /*Uses comma sign as delimiter*/
    char *token=NULL;
    char *temp_stor;
    struct student *array;
    struct student *temp;

    if(argc != 2)
    {
        printf("  usage:  program_name positive_integern");
        printf("example:  ./example-hw09  123n");
        exit(1);
    }

    /**************  begin REQUIRED  **************/
    /*  put before logic.  DO NOT PUT IN A LOOP */
    key = atoi(argv[1]);
    initialize(key);
    /**************   end REQUIRED   **************/

    /*  example loop  */

    array=malloc((init_size)*sizeof(int));

    while(strcmp(value, "stop") != 0)
    {
        value = getString();
        token = strtok(value, del);
        while (token !=NULL)
        {
            if(i%4==0)
            {
                init_size=init_size*2;
                temp = realloc(array,init_size*sizeof(int)) ;
                if(temp != NULL)
                {
                    array = temp;
                }
                else
                {
                    printf("unable to reallocaten");
                    exit(1);
                }
            }

            array[i].name=malloc(sizeof(struct student)*10);
            strcpy(array[i].name,token);
            printf("%s %dn",array[i].name,i);
            token = strtok( NULL, del );
            array[i].course=malloc(sizeof(struct student)*11);
            strcpy(array[i].course,token);
            printf("%s n",array[i].course);
            i=i+1;
            token = strtok( NULL, del );
            x=i;
            for(x=0; x<i; x++)
            {
                if(strcmp(array[x].name,token)==0)
                    printf("Duplicate found n");
            }
        }
    }
}

现在当我尝试执行 strcmp 时,它总是给我一个分段错误,我不知道为什么。

我不应该在这里使用链表,我想我已经完成了所有的事情,对于接下来的几部分我只需要比较和排序,我不断收到分段错误。

我的数组中确实有元素,我可以将它们全部打印出来,只是出于某种原因不比较它们。

【问题讨论】:

  • 你确定学生的名字只有9个字符吗?
  • 请重新粘贴您的代码并删除标签(将您的编辑器配置为仅使用空格并重新缩进)。还要编译你的代码并启用警告(对于 gcc -Wall -Wextra),看看你是否从编译器得到任何提示。
  • 是的,这是作业的一部分,名称不会超过 9 个字母。
  • 你说得对,token是NULL......
  • 您的内存分配似乎几乎是随机选择的 - 您是否了解当您执行以下操作时发生了什么:array=malloc((init_size)*sizeof(int));array[i].name=malloc(sizeof(struct student)*10);?因为用于计算分配大小的表达式似乎与您分配分配的指针的类型没有太大关系。

标签: c arrays struct fault


【解决方案1】:

部分回答,指出没有意义的事情。您应该尝试了解原因,以便修复它们。但 SO 不是解释 malloc 之类的工作原理的正确网站。


    array[i].name=malloc(sizeof(struct student)*10);
    strcpy(array[i].name,token);

您为 10 个student 结构分配空间,然后将字符串复制到其中。这是没有意义的。因为namechar* 你应该有malloc(&lt;maximum size of string with terminating 0 included&gt;)


    array=malloc((init_size)*sizeof(int));

后来

    array[i].name= .....

您将array 分配为整数数组(由sizeof(int) 表示),但随后您将其用作结构体。


然后建议:每次有strcpy(dst, src),都换成这个:

snprintf(dst, <how much space is allocated at dst>, "%s", src);

这将避免缓冲区溢出,并且还会迫使您考虑为dst 分配了多少空间(如果您不知道,那么您的第一个问题需要解决和理解)。

【讨论】:

  • 虽然指出他犯错的具体地方是件好事,但我认为入门课程中的人们需要的一般建议是,他们不应该在不考虑代码行的情况下编写代码行。很明显,OP 不知道 malloc 做了什么,或者至少写了其他意图。如果 OP 无法逐行读取他的代码并实际解释它应该做什么,那么问“为什么strcmp 会引发段错误”是没有意义的。
  • @roliu 是的。我澄清了一点答案。我写信是希望让 OP 去学习这些东西,这样他就可以解决它们。
  • 也不需要这个:snprintf(dst, , "%s", src);因为它没有在课堂上讲过,但是其他的东西,比如 malloc 和 realloc 的真正工作原理有很大帮助,再次感谢你们
【解决方案2】:

因为,很明显(你也说过)令牌为空。

if(strcmp(array[x].name,token)==0)

NULL参数传递给strcmp是非法的。
如果使用 NULL 作为参数调用字符串比较函数,则该进程将获得一个 SIGSEGV,
因为这些函数正在取消引用 NULL 指针。

【讨论】:

  • 你 strtok(NULL,del),把我搞砸了,还以为是在向我传递数据 =/
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-11
  • 1970-01-01
  • 2021-02-07
相关资源
最近更新 更多