使用C中的结构进行分段错误

问题描述 投票:1回答:2

我正在测试我们最近在课堂上学到的关于结构和指针的东西,通过编写一个小的C程序。然而,在运行它后,我遇到了segmentation fault (core dumped)错误。有人可以帮我找出究竟是什么造成的吗?我是否错过任何悬空指针或使用malloc()做错了什么?

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

const char *admin = "Administrator";
const char *mng = "Manager";
const char *prog = "Programmer";

struct employee {
    char *name;
    char title[20];
    char id[8];
    int yearsExp;
};

typedef struct employee emp_t;

emp_t *everyone[1];

emp_t *create_emp(const char *name,const char *title,const char *id,int yrxp) {
    emp_t *new;

    new = (emp_t *) malloc(sizeof(emp_t));
    new->name = (char*) malloc(strlen(name) + 1);
    strcpy(new->name,name);
    strcpy(new->title,title);
    strcpy(new->id,id);
    new->yearsExp = yrxp;
    return new;
}

void free_emp(emp_t *employee) {
    free(employee->name);
    free(employee);
}

int main() {
    int i;

    everyone[0] = create_emp("Mike Papamichail",prog,"A197482",3);
    everyone[1] = create_emp("Maria Mamalaki",mng,"Z104781",6);

    for(i = 0; i < 2;i++) {
        printf("%15s \t %15s \t %10s \t %4d\n",
        everyone[0]->name,everyone[0]->title,everyone[0]->id,everyone[0]->yearsExp);
        free_emp(everyone[i]);
        everyone[i] = NULL;
    }
    return 0;
}

更新代码以便清楚

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

const char *admin = "Administrator";
const char *mng = "Manager";
const char *prog = "Programmer";

struct employee {
    char *name;
    char title[20];
    char id[8];
    int yearsExp;
};

typedef struct employee emp_t;

emp_t *create_emp(const char *name,const char *title,const char *id,int yrxp) {
    emp_t *new;

    new = (emp_t *) malloc(sizeof(emp_t));
    new->name = (char*) malloc(strlen(name) + 1);
    strcpy(new->name,name);
    strcpy(new->title,title);
    strcpy(new->id,id);
    new->yearsExp = yrxp;
    return new;
}

void free_emp(emp_t *employee) {
    free(employee->name);
    free(employee);
}

int main() {
    int i;
    emp_t *everyone[2];

    everyone[0] = create_emp("Mike Papamichail",prog,"A197482",3);
    everyone[1] = create_emp("Maria Mamalaki",mng,"Z104781",6);

    for(i = 0; i < 2;i++) {
        printf("%15s \t %15s \t %10s \t %4d\n",
        everyone[0]->name,everyone[0]->title,everyone[0]->id,everyone[0]->yearsExp);
        free_emp(everyone[i]);
        everyone[i] = NULL;
    }
    return 0;
}
c pointers struct segmentation-fault
2个回答
3
投票

你在所有点上都很接近@tadman在使用emp_t *everyone[1]创建一个1指针数组时遇到了最大的错误(现在已在你的问题中删除)。

与代码相关的其余问题更多是相对而言的一些小修正或改进。

对于初学者,请避免在代码中使用“魔术数字”(例如20, 8, 2)。如果您需要常量,#define它们,或使用全局enum一次定义多个常量,例如

/* an enum can be used to #define multiple constants */
enum { MAXID = 8, MAXTITLE = 20, MAXEMP = 128 };

然后

typedef struct {    
    char *name;
    char title[MAXTITLE];   /* avoid "magic-numbers", use constants */
    char id[MAXID];
    int yearsExp;
} emp_t;

你的create_emp()函数在很大程度上可以正常工作,但是没有必要在C中输出malloc, callocrealloc的回报,参见:Do I cast the result of malloc?。另外,我会避免使用new作为临时结构名称。虽然C中没有实际的冲突,但new是C ++中的关键字,如果你要在两者中编码,最好记住这一点。通过几次调整,您可以按如下方式编写create_emp()

emp_t *create_emp (const char *name, const char *title, 
                    const char *id, int yrxp) {
    emp_t *newemp;  /* new is a keyword in C++ best to avoid */

    newemp = malloc (sizeof *newemp);   /* don't cast return of malloc */
    /* if you allocate, you must validate */
    if (newemp == NULL) {
        perror ("malloc-newemp");
        return NULL;
    }
    newemp->name = malloc (strlen(name) + 1);
    if (newemp->name == NULL) {
        perror ("malloc-newemp->name");
        free (newemp);
        return NULL;
    }
    strcpy (newemp->name, name);
    strcpy (newemp->title, title);
    strcpy (newemp->id, id);
    newemp->yearsExp = yrxp;

    return newemp;
}

(注意:始终验证每个分配.malloc, calloc & realloc可以并且确实失败)

最后,在main()中,您可以使用索引(下面的ndx)而不是魔术数字2,并在每次添加时增加索引。虽然您使用字段宽度修饰符来控制输出字段大小,但对于字符串(以及所有转换说明符),您可以将'-'标志包含在转换说明符的一部分中,以使字段Left-Justified正确对齐输出。你的代码的其余部分没问题。

完全放在一起,您可以执行以下操作:

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

/* an enum can be used to #define multiple constants */
enum { MAXID = 8, MAXTITLE = 20, MAXEMP = 128 };
#define ADMIN   "Administrator" /* simple #defines are fine */
#define MNG     "Manager"       /* string literals are fine as well */
#define PROG    "Programmer"    /* (up to you) */

typedef struct {    
    char *name;
    char title[MAXTITLE];   /* avoid "magic-numbers", use constants */
    char id[MAXID];
    int yearsExp;
} emp_t;

emp_t *create_emp (const char *name, const char *title, 
                    const char *id, int yrxp) {
    emp_t *newemp;  /* new is a keyword in C++ best to avoid */

    newemp = malloc (sizeof *newemp);   /* don't cast return of malloc */
    /* if you allocate, you must validate */
    if (newemp == NULL) {
        perror ("malloc-newemp");
        return NULL;
    }
    newemp->name = malloc (strlen(name) + 1);
    if (newemp->name == NULL) {
        perror ("malloc-newemp->name");
        free (newemp);
        return NULL;
    }
    strcpy (newemp->name, name);
    strcpy (newemp->title, title);
    strcpy (newemp->id, id);
    newemp->yearsExp = yrxp;

    return newemp;
}

void free_emp (emp_t *employee) {
    free (employee->name);
    free (employee);
}

int main (void) {

    int i, ndx = 0;     /* use an index instead of magic-numbers */

    emp_t *everyone[MAXEMP] = {NULL};

    everyone[ndx++] = create_emp ("Mike Papamichail", PROG, "A197482", 3);
    everyone[ndx++] = create_emp ("Maria Mamalaki", MNG, "Z104781", 6);
    everyone[ndx++] = create_emp ("Sam Sunami", ADMIN, "B426310", 10);

    for (i = 0; i < ndx; i++) { /* %- to left justify fields */
        if (everyone[i]) {      /* validate not NULL */
            printf ("%-15s \t %-15s \t %-10s \t %4d\n", everyone[i]->name, 
                    everyone[i]->title, everyone[i]->id, 
                    everyone[i]->yearsExp);
            free_emp (everyone[i]);
            everyone[i] = NULL;
        }
    }

    return 0;
}

(注意:从everyone[0]->nameeveryone[i]->name的变化等等,否则输出永远不会改变)

示例使用/输出

$ ./bin/emp_struct
Mike Papamichail         Programmer              A197482            3
Maria Mamalaki           Manager                 Z104781            6
Sam Sunami               Administrator           B426310           10

仔细看看,如果您有其他问题,请告诉我。


0
投票

最后,你循环遍历你的阵列,打印everyone[0],然后释放everyone[i]。所以在第二次迭代中,everyone[0]将为null并且代码崩溃...

© www.soinside.com 2019 - 2024. All rights reserved.