【发布时间】:2015-06-24 03:25:28
【问题描述】:
我的代码使用联合来表示一个 RGB 像素。它在调试模式下运行良好,但启用编译器优化后,它会出现段错误。
您可以查看下面的简化测试代码来重现错误。我的系统是带有 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;
}
【问题讨论】:
-
我不建议在 C 代码(尤其是头文件内部)中使用 C++ 关键字(如
this)。 -
typedef struct Screen *Screen;,这真的很混乱。最好不要使用指针类型定义;但如果必须,请给它们一个不同于它们指向的类型的名称。
标签: c segmentation-fault return