在C语言中使用structs进行故障分割
我正在通过编写一个小型C程序来测试我们最近在课堂上学到的关于结构和指针的知识。然而,在运行它之后,我遇到了一个在C语言中使用structs进行故障分割,c,pointers,struct,segmentation-fault,C,Pointers,Struct,Segmentation Fault,我正在通过编写一个小型C程序来测试我们最近在课堂上学到的关于结构和指针的知识。然而,在运行它之后,我遇到了一个分段错误(内核转储)错误。有人能帮我弄清楚到底是什么原因造成的吗?我是否错过了任何悬空的指针,或者使用malloc(),我是否做错了什么 #包括 #包括 #包括 const char*admin=“管理员”; const char*mng=“经理”; const char*prog=“程序员”; 结构雇员{ 字符*名称; 字符标题[20]; 字符id[8]; 国际年性别政策; }; 类型
分段错误(内核转储)
错误。有人能帮我弄清楚到底是什么原因造成的吗?我是否错过了任何悬空的指针,或者使用malloc()
,我是否做错了什么
#包括
#包括
#包括
const char*admin=“管理员”;
const char*mng=“经理”;
const char*prog=“程序员”;
结构雇员{
字符*名称;
字符标题[20];
字符id[8];
国际年性别政策;
};
类型定义结构员工管理;
emp_t*所有人[1];
emp\u t*创建\u emp(常量字符*名称,常量字符*标题,常量字符*id,int yrxp){
环境管理计划*新;
新=(emp_t*)malloc(sizeof(emp_t));
新建->名称=(字符*)malloc(strlen(名称)+1);
strcpy(新建->名称,名称);
strcpy(新建->标题,标题);
strcpy(新建->标识,标识);
新->年SEXP=yrxp;
归还新的;
}
无效自由emp(emp*t员工){
免费(员工->姓名);
免费(员工);
}
int main(){
int i;
everyone[0]=create_emp(“Mike Papamichail”,prog,“A197482”,3);
每个人[1]=创建emp(“Maria Mamalaki”,mng,“Z104781”,6);
对于(i=0;i<2;i++){
printf(“%15s\t%15s\t%10s\t%4d\n”,
每个人[0]->姓名,每个人[0]->头衔,每个人[0]->id,每个人[0]->yearsExp);
自由(每个人[我]);
everyone[i]=NULL;
}
返回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;
}
#包括
#包括
#包括
const char*admin=“管理员”;
const char*mng=“经理”;
const char*prog=“程序员”;
结构雇员{
字符*名称;
字符标题[20];
字符id[8];
国际年性别政策;
};
类型定义结构员工管理;
emp\u t*创建\u emp(常量字符*名称,常量字符*标题,常量字符*id,int yrxp){
环境管理计划*新;
新=(emp_t*)malloc(sizeof(emp_t));
新建->名称=(字符*)malloc(strlen(名称)+1);
strcpy(新建->名称,名称);
strcpy(新建->标题,标题);
strcpy(新建->标识,标识);
新->年SEXP=yrxp;
归还新的;
}
无效自由emp(emp*t员工){
免费(员工->姓名);
免费(员工);
}
int main(){
int i;
emp_t*所有人[2];
everyone[0]=create_emp(“Mike Papamichail”,prog,“A197482”,3);
每个人[1]=创建emp(“Maria Mamalaki”,mng,“Z104781”,6);
对于(i=0;i<2;i++){
printf(“%15s\t%15s\t%10s\t%4d\n”,
每个人[0]->姓名,每个人[0]->头衔,每个人[0]->id,每个人[0]->yearsExp);
自由(每个人[我]);
everyone[i]=NULL;
}
返回0;
}
您已接近所有点,在使用emp\u t*everyone[1]
创建一个由1个指针组成的数组时,您发现了最大的错误(现在已在您的问题中删除)
相对而言,代码的其余问题更多的是一些小的修改或改进
对于初学者,避免在代码中使用“幻数”(例如20,8,2
)。如果需要常量,请定义它们,或使用全局枚举一次定义多个常量,例如
/* 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
的返回,请参阅:。另外,我将避免使用new
作为临时结构名。C中没有实际冲突,<代码>新< /Cord>是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
can和do失败)
最后,在main()。在使用字段宽度修饰符控制输出字段大小方面做得不错,但对于字符串(以及所有转换说明符),可以将'-'
标志作为转换说明符的一部分,使字段左对齐以正确对齐输出。代码的其余部分很好
总而言之,您可以执行以下操作:
#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;
}
仔细检查一下,如果您还有其他问题,请告诉我。最后,您将在数组中循环,打印所有人[0]
,然后释放所有人[i]
。因此在第二次迭代中,everyone[0]
将为空,代码将崩溃……提示:emp\t*everyone[1]
创建一个长度为1的数组,因此最大索引为0<代码>所有人[1]
都超出范围。@罗伯特哈维,这是C。你可以做任何事情,尤其是那个。C假设当你要求超限时你知道自己在做什么,所以作为程序员,如果你不想有未定义的行为,你就应该不这样做。我建议动态分配的原因是大小声明和填充代码并存,因此错误更明显。@tadman第一部分你是对的,我已经在代码中更改了它。至于第二部分,我将尽快对代码进行必要的更改!为什么要在数组上调用malloc
,然后在调用顶部的create\u emp
时将其丢弃?你需要抛弃的一个习惯是施放你的malloc
结果。没必要。让编译器来处理这个问题。是的,很遗憾,计算机科学教授有一个很长的传统,他们会这么说。你需要更好地解释什么具体的事情?非常感谢你的回答,因为在这里已经太晚了,我会在早上复习并在这里再次发布,希望你不介意。再次感谢!没问题,注意:我为malloc
添加了额外的验证,然后在输出循环中添加了验证(这通常是在填充循环中进行的,但这里是硬编码的),非常感谢您的回答和经验
#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;
}
$ ./bin/emp_struct
Mike Papamichail Programmer A197482 3
Maria Mamalaki Manager Z104781 6
Sam Sunami Administrator B426310 10