我正在通过编写一个小型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;
}
答案 0 :(得分: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, calloc
或realloc
的返回值,请参见: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
)代替 magic-number 2
,并在每次添加后增加索引。当您很好地使用 field-width 修饰符来控制输出字段的大小时,对于字符串(和所有转换说明符),可以将'-'
标志作为转换说明符的一部分包含在以下内容中:将字段设置为 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]->name
更改为everyone[i]->name
,依此类推,否则输出将永远不变)
使用/输出示例
$ ./bin/emp_struct
Mike Papamichail Programmer A197482 3
Maria Mamalaki Manager Z104781 6
Sam Sunami Administrator B426310 10
仔细检查一下,如果还有其他问题,请告诉我。
答案 1 :(得分:0)
最后,您要遍历数组,打印everyone[0]
,然后释放everyone[i]
。因此,在第二次迭代中,everyone[0]
将为null,并且代码崩溃...