[英]Segmentation fault using structs in C
我正在测试我们最近在课堂上学到的关于结构和指针的东西,通过编写一个小的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;
}
你接近所有点@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
,请参阅: 我是否转换了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
并在每次添加时增加索引。 虽然您使用字段宽度修饰符来控制输出字段大小,但对于字符串(以及所有转换说明符),您可以在转换说明符中包含'-'
标志,以使字段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
仔细看看,如果您有其他问题,请告诉我。
最后,你循环遍历你的阵列,打印everyone[0]
,然后释放everyone[i]
。 所以在第二次迭代中, everyone[0]
都将为null并且代码崩溃...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.