我遇到分段错误问题。当我在带有devc ++或codeblocks的Windows机器上运行这个特定的代码时,它运行我的意图。当我尝试在Linux机器上运行时会出现问题。有人告诉我,这很可能是由于指针试图访问不允许的位置,但我无法弄清楚原因。
当我在没有" empinfo.txt"的情况下运行程序时文件它将启动并允许我执行任何菜单选项,除了" 1-Add",所以问题似乎与我使用我的结构的数组的方式有关。
我包含了所有代码,但唯一的问题是函数(我认为)是初始化和添加。在这种情况下,使用一系列字符的正确方法的任何帮助都将受到高度赞赏。
以下是empinfo.txt文件中的示例输入
12 JackSprat 2 1 65000
13 HumptyDumpty 5 3 30000
17 BoPeep 2 3 30000
20 BoyBlue 3 2 58000
0
-
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
FILE *empinfo=NULL;
struct employeeData {
int EMP_ID;
char name[20];
int dept;
int rank;
double salary;
struct employeeData *next;
};
struct employeeData *head = NULL;
void initializeList();
void add(int ID, char name[0], int dept, int rank, double newSalary);
void deleteEmp(int ID);
void modify(int ID, double NewSalary);
void query(int rank);
void print();
int main(){
int choice=1, ID=0, rank=0, dept=0;
double newSalary=0;
char name[20];
initializeList();
while (choice != 0) {
printf("1-Add 2-Delete 3-Modify 4-Query 5-Print 0-Exit\n");
fflush(stdin);
scanf("%d", &choice);
if (choice == 1) {
printf("Enter new employee info. Format: \"ID name dept rank salary\"\n");
if((scanf("%d %s %d %d %lf", &ID, name, &dept, &rank, &newSalary))==5)
add(ID, name, dept, rank, newSalary);
else{
printf("invalid format entered\n\n");
continue;
}
}
else if (choice == 2){
printf("Enter employee ID to delete: ");
if(scanf("%d", &ID)==1){
deleteEmp(ID);}
else{
printf("Employee ID must be an integer\n");
continue;
}
}
else if (choice == 3){
printf("Enter employee ID to modify: ");
if(scanf("%d", &ID)==1){
printf("Enter new salary amount: ");
if(scanf("%lf", &newSalary)==1)
modify(ID,newSalary);
else
printf("Salary must be a number\n");
}
else{
printf("Employee ID must be an integer\n");
continue;
}
}
else if (choice == 4){
printf("Enter the rank you wish to query: ");
scanf("%d", &rank);
query(rank);
}
else if (choice == 5){
print();
}
}
printf("Goodbye...\n");
head=NULL;
free(head);
return 0;
}
void initializeList(){
empinfo=fopen("empinfo.txt", "r");
head = (struct employeeData *)malloc(sizeof(struct employeeData));
head->next = NULL;
if (empinfo==NULL){
printf("empinfo.txt not found, file not opened.\n");
head=NULL;
free(head);
free(empinfo);
return;
}
struct employeeData *tempPtr = head;
while (tempPtr->EMP_ID != 0){
fscanf(empinfo, "%d %s %d %d %lf", &tempPtr->EMP_ID, tempPtr->name, &tempPtr->dept, &tempPtr->rank, &tempPtr->salary);
if (tempPtr->EMP_ID == 0){
break;
}
tempPtr->next = (struct employeeData *)malloc(sizeof(struct employeeData));
tempPtr=tempPtr->next;
}
tempPtr=head;
while(tempPtr->next->EMP_ID!=0){
tempPtr=tempPtr->next;
}
empinfo=NULL;
free(empinfo);
tempPtr->next=NULL;
fclose(empinfo);
tempPtr=NULL;
free(tempPtr);
}
void add(int ID, char name[], int dept, int rank, double newSalary){
struct employeeData *tempPtr = head;
while ((tempPtr->next!=NULL) && (tempPtr->next->EMP_ID < ID)) {
tempPtr=tempPtr->next;
}
struct employeeData *newNode = (struct employeeData * )malloc(sizeof(struct employeeData));
newNode->EMP_ID = ID;
strcpy(newNode->name,name);
newNode->dept = dept;
newNode->rank = rank;
newNode->salary = newSalary;
newNode->next=NULL;
if (tempPtr==head) {
if(ID>tempPtr->EMP_ID){
newNode->next = tempPtr->next;
tempPtr->next = newNode;
}
else {
newNode->next=tempPtr;
head=newNode;
}
}
else if (tempPtr->next == NULL){
tempPtr->next=newNode;
newNode->next=NULL;
}
else{
newNode->next = tempPtr->next;
tempPtr->next = newNode;
}
printf("Employee #%d has been added to the database.\n\n", ID);
tempPtr=NULL;
newNode=NULL;
free(newNode);
free(tempPtr);
}
void deleteEmp(int ID){
struct employeeData *seek=head,*tempPtr = head;
if(head!=NULL) {
while((tempPtr->EMP_ID!=ID)){
seek = tempPtr;
tempPtr=tempPtr->next;
if(tempPtr==NULL){
printf("Employee ID not found\n");
free(tempPtr);
seek=NULL;
free(seek);
return;
}
}
if (tempPtr!=NULL){
if(tempPtr==head){
if(tempPtr->next==NULL){
printf("List cannot be empty\n");
free(tempPtr);
seek=NULL;
free(seek);
return;
}
head=tempPtr->next;
free(tempPtr);
}
else if (tempPtr->next==NULL){
free(tempPtr);
seek->next = NULL;
}
else{
seek->next=tempPtr->next;
free(tempPtr);
}
}
}
printf("Employee #%d has been deleted\n", ID);
tempPtr=NULL;
free(tempPtr);
seek=NULL;
free(seek);
}
void modify(int ID, double NewSalary){
struct employeeData *tempPtr = head;
while (tempPtr!=NULL&&tempPtr->EMP_ID!=ID){
tempPtr=tempPtr->next;
}
if(tempPtr==NULL){
printf("Employee ID not found\n");
free(tempPtr);
return;
}
tempPtr->salary=NewSalary;
printf("Employee salary updated.\n\n");
tempPtr=NULL;
free(tempPtr);
}
void query(int rank){
struct employeeData *tempPtr = head;
while (tempPtr!=NULL){
if(tempPtr->rank == rank){
printf("%s\n", tempPtr->name);
tempPtr=tempPtr->next;
}
else
tempPtr=tempPtr->next;
}
tempPtr=NULL;
free(tempPtr);
}
void print(){
struct employeeData *tempPtr = head;
while (tempPtr!=NULL){
printf("%d %s %d %d %.0lf\n", tempPtr->EMP_ID, tempPtr->name, tempPtr->dept, tempPtr->rank, tempPtr->salary);
tempPtr=tempPtr->next;
}
free(tempPtr);
答案 0 :(得分:2)
使用调试器。如果你在Linux上,你可能有权访问gdb。这是一个与您的示例和提供的测试文件的会话:
$ gdb test
...
(gdb) run
...
Program received signal SIGSEGV, Segmentation fault.
0x0000000000400bb2 in initializeList () at test.c:111
111 while(tempPtr->next->EMP_ID!=0){
Missing separate debuginfos, use: debuginfo-install glibc-2.18-14.fc20.x86_64
(gdb) p tempPtr
$1 = (struct employeeData *) 0x603250
(gdb) p tempPtr->next
$2 = (struct employeeData *) 0x0
我们可以看到我们正在尝试取消引用第111行的NULL指针。应该很容易找到这个信息的问题。
这也肯定会导致错误:
empinfo=fopen(...)
...
empinfo=NULL;
free(empinfo);
fclose(empinfo);
你不应该释放你从fopen
得到的句柄,你不应该把它设置为NULL,让fclose处理它。
答案 1 :(得分:0)
欢迎来到未定义行为的世界!除了很多其他事情,如果你这样做,UB就会发生:
The value of the object allocated by the malloc function is used (7.20.3.3).
在C99 standard中了解更多信息,请特别注意附件J.这不是一个有趣的阅读,但如果您想在任何其他环境中使用C语言而不是玩游戏,则必不可少。
所以你发生的事情是,在Windows平台上,malloc()
返回的内存显然已初始化为非零,包括tempPtr->EMP_ID
。这就是它在那里工作的原因,但由于它仍然是未定义的行为,它仍然是错误的!
在Linux上,内存显然设置为全0,导致while()
中的initializeList()
循环被跳过,导致tempPtr->next
的取消引用被设置为NULL
只是一个之前几行...
由于您的代码中出现了很多错误,因此这里提供了一些提示:
calloc()
代替malloc()
。这会将内存初始化为全0 NULL
memset()
数组fscanf()
的文档。从来没有自己使用它,但你使用它不安全(缓冲区溢出)free()
的每次使用都有意义。把它放在你认为可能是明智的地方的任何地方都不是一个好的编码实践:)。将它用于文件描述符是错误的。