执行完所有内容后程序末尾出现段错误?
我编写了一个快速程序,它在给出段错误之前执行每个语句。
struct foo
{
int cat;
int * dog;
};
void bar (void * arg)
{
printf("o hello bar\n");
struct foo * food = (struct foo *) arg;
printf("cat meows %i\n", food->cat);
printf("dog barks %i\n", *(food->dog));
}
void main()
{
int cat = 4;
int * dog;
dog = &cat;
printf("cat meows %i\n", cat);
printf("dog barks %i\n", *dog);
struct foo * food;
food->cat = cat;
food->dog = dog;
printf("cat meows %i\n", food->cat);
printf("dog barks %i\n", *(food->dog));
printf("time for foo!\n");
bar(food);
printf("begone!\n");
cat = 5;
printf("cat meows %i\n", cat);
printf("dog barks %i\n", *dog);
// return 0;
}
这给出了
cat meows 4
dog barks 4
cat meows 4
dog barks 4
time for foo!
o hello bar
cat meows 4
dog barks 4
begone!
cat meows 5
dog barks 5
Segmentation fault (core dumped)
我不太确定为什么它最后会出现段错误的结果?任何评论/见解都深表感谢。
I wrote a quick program which executes every statement before giving a seg fault error.
struct foo
{
int cat;
int * dog;
};
void bar (void * arg)
{
printf("o hello bar\n");
struct foo * food = (struct foo *) arg;
printf("cat meows %i\n", food->cat);
printf("dog barks %i\n", *(food->dog));
}
void main()
{
int cat = 4;
int * dog;
dog = &cat;
printf("cat meows %i\n", cat);
printf("dog barks %i\n", *dog);
struct foo * food;
food->cat = cat;
food->dog = dog;
printf("cat meows %i\n", food->cat);
printf("dog barks %i\n", *(food->dog));
printf("time for foo!\n");
bar(food);
printf("begone!\n");
cat = 5;
printf("cat meows %i\n", cat);
printf("dog barks %i\n", *dog);
// return 0;
}
which gives a result of
cat meows 4
dog barks 4
cat meows 4
dog barks 4
time for foo!
o hello bar
cat meows 4
dog barks 4
begone!
cat meows 5
dog barks 5
Segmentation fault (core dumped)
I'm not really sure why it seg faults at the end? Any comments/insights are deeply appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您正在取消引用指向无效内存
food
的指针。该行:
声明 food 是指向 struct foo 的指针。但由于您没有初始化指针,因此它指向您不拥有的未定义的内存区域。您可以只在堆栈上分配(注意我已经更改了食物的类型):
或者使用 malloc(将类型保持为 struct foo *):
稍后您应该释放它(尽管在这种情况下)这并不重要):
程序还有其他问题(例如 void* 参数),但这解决了内存违规问题。
You're dereferencing a pointer to invalid memory,
food
.The line:
declares food to be a pointer to a struct foo. But since you're not initializing the pointer, it's pointing to an undefined area of memory you don't own. You can either just allocate on the stack (note I've changed the type of food):
or use
malloc
(keeping the type as struct foo *):Later you should free it (though in this case it doesn't matter much):
There are other issues with the program (e.g. the void* parameter), but this addresses the memory violation.
好吧,这些行是一个问题:
Food 是一个指针,您可以取消引用该指针,而无需将其分配给任何东西。
可能会为你解决问题。
Well, these lines are a problem:
Food is a pointer which you dereference without it being assigned to anything.
might fix things for you.
您尚未为变量
food
指向的结构变量分配空间:您需要执行以下
操作:一旦使用完该内存,您还应该释放该内存:
或者您可以将 food 声明为
struct foo
类型的变量为:,然后您可以使用
.
运算符代替->
运算符来访问结构成员。所以更改为
You have not allocated space for a structure variable to be pointed by the variable
food
:You need to do:
also you should be deallocating this memory once you are done using it:
Alternative you can declare food as a variable of type
struct foo
as:and then you can access the structure members using the
.
operator inplace of the->
operator. SoChanges to
你会得到未定义的行为,因为你没有 malloc 食物结构。
You're getting undefined behavior because you didn't malloc the food struct.