gpt4 book ai didi

c - 与编译器优化结合的段错误

转载 作者:行者123 更新时间:2023-12-02 01:00:08 25 4
gpt4 key购买 nike

我的代码使用 union 来表示 RGB 像素。它在 Debug模式下运行良好,但启用编译器优化后,它会出现段错误。

您可以在下面看到用于重现错误的简化测试代码。我的系统是带有 GCC4.8.2 的 Ubuntu。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>

union Pixel {
uint32_t color;
uint8_t at[4];
};

typedef struct Screen {
union Pixel *pixels;
int width;
int height;
int rPosition;
int gPosition;
int bPosition;
} *Screen;

Screen ScreenCreate(int width, int height, uint32_t rPosition, uint32_t gPosition, uint32_t bPosition) {
Screen this = malloc(sizeof *this);
this->pixels = malloc(width * height * sizeof this->pixels[0]);
this->width = width;
this->height = height;
this->rPosition = rPosition;
this->gPosition = gPosition;
this->bPosition = bPosition;
}

void ScreenDelete(Screen this) {
free(this->pixels);
free(this);
}

void ScreenFill(Screen this, uint8_t r, uint8_t g, uint8_t b) {
union Pixel pixel;
pixel.color = 0;
pixel.at[this->rPosition] = r;
pixel.at[this->gPosition] = g;
pixel.at[this->bPosition] = b;
for (int i = 0; i < this->width * this->height; i += 1) {
this->pixels[i].color = pixel.color;
}
}

int main(void) {
Screen screen = ScreenCreate(500, 500, 2, 1, 0);
ScreenFill(screen, 0xff, 0xff, 0xff);
ScreenDelete(screen);
return 0;
}

最佳答案

在您的 ScreenCreate() 函数中,您忘记了 return 语句。因此,您的代码会产生未定义的行为。

最后一行应该是

 return this;

相关:根据 C11 标准,第 6.9.1 章,函数定义,第 12 段

If the } that terminates a function is reached, and the value of the function call is used by the caller, the behavior is undefined.

因此,在 main() 中使用 screen 导致 undefined behaviour .

故事的寓意:启用编译器警告并留意它们。

关于c - 与编译器优化结合的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29696455/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com