简体   繁体   English

C结构中的总线错误

[英]Bus error in C struct

This is a code I testing to copying struct. 这是我测试复制struct的代码。

 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 typedef struct emp_struct {
 5     char *name;
 6     int employee_no;
 7     float salary,
 8           tax_to_date;
 9 } Employee;
 10 
 11 typedef Employee Database[10];
 12 
 13 Database people = {
 14     {"Fred", 10, 10000, 3000},
 15     {"Jim", 9, 12000, 3100.5},
 16     {"Fred", 13, 1000000, 30},
 17     {"Mary", 11, 170000, 4000},
 18     {"Judith", 45, 130000, 50000},
 19     {"Nigel", 10, 5000, 1200},
 20     {"Trevor", 10, 20000, 6000},
 21     {"Karen", 10, 120000, 34000},
 22     {"Marianne", 10, 50000, 12000},
 23     {"Mildred", 10, 100000, 30000}
 24 };
 25 
 26 int main () {
 27     // array act like pointer, thus pointing + pointing = ERROR
 28     printf("people[1]->name: ERROR\n");
 29     // jump its memory amount of struct size
 30     printf("(people+1)->name:%s\n",(people+1)->name);
 31     // array works as a pointer
 32     printf("people[3].name:%s\n",people[3].name);
 33 
 34     /* Is it possible to assign struct to struct? */
 35     printf("\nAssigning struct to struct\n");
 36     Employee temp;
 37     temp = *(people+5); // Nigel Record
 38     printf("Name: %s\n",temp.name);
 39     // exchange
 40     strcpy(temp.name, "Ahn");
 41     printf("Changed: %s\n",temp.name);
 42     printf("Original: %s\n",people[5].name);
 43 
 44     return 0;
 45 }

When I tried to strcpy(new, string) in line 40, then it throws out Bus error: 10 . 当我尝试在第40行中strcpy(new,string)时,它会抛出Bus错误:10

I expected that value Changed and Original are same in line 41 and 42. But it would not work. 我预计第41和42行的值ChangedOriginal是相同的。但它不起作用。 What is the problem of assigning? 分配有什么问题?

I knew that Bus error:10 is occurred by lack of assignment space. 我知道总线错误:10是由于缺少任务空间而发生的。 But my name field in struct is pointer (in line 5). 但我在struct中的名字字段是指针(在第5行)。 If I changed name field like 如果我改名字段就好了

 char name[100];

It works properly and Changed and Original value are different! 它工作正常, ChangedOriginal值不同! Even though I assigned that as a pointer. 即使我将其指定为指针。

What is the problem of this struct assigning? 这个结构分配有什么问题?

temp.name is a pointer to char. temp.name是指向char的指针。 After the copy temp = *(people+5); 复制后temp = *(people+5); , temp.name is pointing to the bytes containing "Nigel". ,temp.name指向包含“Nigel”的字节。

So far, no problem. 到目前为止,没问题。 But you can't use that pointer as the output for strcpy(); 但你不能使用该指针作为strcpy()的输出; strcpy will try to overwrite it, and it's stored in read-only memory. strcpy将尝试覆盖它,并将其存储在只读内存中。

You can do this: 你可以这样做:

temp.name = "Ahn"; temp.name =“Ahn”;

... since all that you are doing here is changing the pointer (temp.name) to point to a different area of memory which is set up in advance to contain "Ahn". ...因为你在这里所做的就是改变指针(temp.name)以指向预先设置的包含“Ahn”的不同内存区域。

This is actually nothing to do with struct copying. 这实际上与结构复制无关。 You will have the same problem if you try to do strcpy(people[3].name, "Ahn") 如果你试图做strcpy(people[3].name, "Ahn")你会遇到同样的问题strcpy(people[3].name, "Ahn")

When you declared the Database record, all the names(Fred, Jim, Fred ...) were put in the read-only memory of the data segment and the name character pointer pointed to their starting addresses. 声明数据库记录时,所有名称(Fred,Jim,Fred ...)都放在数据段的read-only内存中, name字符指针指向它们的起始地址。

When you do strcpy(temp.name, "Ahn"); 当你做strcpy(temp.name, "Ahn"); , you are trying to write in the read-only. ,你试图用只读方式写。 Trying to write into read-only memory will cause a bus error. 尝试写入read-only内存会导致总线错误。

The solution would be to allocate memory for the name and then do the strcpy . 解决方案是为name分配内存,然后执行strcpy Also a good programming practice is to use strncpy instead of strcpy 一个好的编程习惯是使用strncpy而不是strcpy

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM