执行完所有内容后程序末尾出现段错误?

发布于 2024-08-27 22:13:16 字数 1016 浏览 8 评论 0原文

我编写了一个快速程序,它在给出段错误之前执行每个语句。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我也只是我 2024-09-03 22:13:16

您正在取消引用指向无效内存 food 的指针。

该行:

struct foo * food;

声明 food 是指向 struct foo 的指针。但由于您没有初始化指针,因此它指向您不拥有的未定义的内存区域。您可以只在堆栈上分配(注意我已经更改了食物的类型):

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);

或者使用 malloc(将类型保持为 struct foo *):

struct foo * food = malloc(sizeof(struct foo));
if(food == NULL)
  perror("Failed to allocate food.");

稍后您应该释放它(尽管在这种情况下)这并不重要):

free(food);

程序还有其他问题(例如 void* 参数),但这解决了内存违规问题。

You're dereferencing a pointer to invalid memory, food.

The line:

struct foo * food;

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):

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);

or use malloc (keeping the type as struct foo *):

struct foo * food = malloc(sizeof(struct foo));
if(food == NULL)
  perror("Failed to allocate food.");

Later you should free it (though in this case it doesn't matter much):

free(food);

There are other issues with the program (e.g. the void* parameter), but this addresses the memory violation.

若沐 2024-09-03 22:13:16

好吧,这些行是一个问题:

struct foo * food;
food->cat = cat;
food->dog = dog;

Food 是一个指针,您可以取消引用该指针,而无需将其分配给任何东西。

struct foo food;
food.cat = cat;
food.dog = dog;

可能会为你解决问题。

Well, these lines are a problem:

struct foo * food;
food->cat = cat;
food->dog = dog;

Food is a pointer which you dereference without it being assigned to anything.

struct foo food;
food.cat = cat;
food.dog = dog;

might fix things for you.

落在眉间の轻吻 2024-09-03 22:13:16

您尚未为变量 food 指向的结构变量分配空间:

struct foo * food;

您需要执行以下

struct foo * food = (struct foo*) malloc(sizeof(struct foo));

操作:一旦使用完该内存,您还应该释放该内存:

free(food);

或者您可以将 food 声明为struct foo 类型的变量为:,

struct foo food; // note the missing *

然后您可以使用 . 运算符代替 -> 运算符来访问结构成员。所以

food->cat = cat;

更改为

food.cat = cat;

You have not allocated space for a structure variable to be pointed by the variable food:

struct foo * food;

You need to do:

struct foo * food = (struct foo*) malloc(sizeof(struct foo));

also you should be deallocating this memory once you are done using it:

free(food);

Alternative you can declare food as a variable of type struct foo as:

struct foo food; // note the missing *

and then you can access the structure members using the . operator inplace of the -> operator. So

food->cat = cat;

Changes to

food.cat = cat;
浪漫人生路 2024-09-03 22:13:16

你会得到未定义的行为,因为你没有 malloc 食物结构。

You're getting undefined behavior because you didn't malloc the food struct.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文