c++ - `pragma pack(push, 1)` 在 GCC 4.4.7 中崩溃。可能的编译器错误?

标签 c++ gcc pragma gcc4

我遇到了一个让我难过的错误。我已将其缩小为 GCC(特别是 RHEL Linux,GCC v.4.4.7)中的 pragma pack 命令的问题,可以在下面显示的小示例案例中重新创建该问题。看起来 GCC 在这种情况下计算了错误的偏移量,这将表现为循环内的崩溃。删除 pragma pack 也可以消除错误 - 但在实际应用程序中,这将导致使用许多额外的千兆字节内存,这是不可取的。

在下面的示例中,您需要在启用优化 (O3) 的情况下进行编译才能体验失败。我还在结构中提供了一个示例项 (cMagic),可以将其删除,这将更改结构对齐方式并防止错误触发。

我查看了生成的程序集,认为这可能是一个编译器错误。我还缺少其他东西吗?任何人都可以确认此错误或提供任何见解吗?

崩溃.cpp:

/*  Platform Version Info:
 *     gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-16)
 *     uname: 2.6.32-504.16.2.el6.x86_64 #1 SMP Tue Mar 10 17:01:00 EDT 2015 x86_64 x86_64 x86_64 GNU/Linux
 *
 *  Compiling:
 *     Must use -O3 for compiling and linking
 *     CXX= g++ -g -O3 -fPIC -rdynamic -Wall -Wno-deprecated -DDEBUG
 *     CPP= g++ -g -O3 -fPIC -rdynamic -Wall -Wno-deprecated -DDEBUG
 *
 *  Notes:
 *     This appears to be an optimization and alignment issue.
 *     Getting rid of a byte in Place (cMagic) causes the program to complete successfully.
 *
 */


#include <stdlib.h>
#include <iostream>

using namespace std;

#pragma pack(push,1)  // Structures must be packed tightly
#define MAGICCONSTANT 17

struct Place {
   int iFoo;
   char cMagic;         // GCC doesn't like cMagic.  Disillusion it and everything is OK
   int aiArray[MAGICCONSTANT];
};


#pragma pack(pop)

int main(int argc, const char *argv[])
{
   Place *pPlace = new Place;   // Place must be on the heap... so new, calloc, malloc, etc

   for (int c = 0; (c < MAGICCONSTANT); c++) {
      pPlace->aiArray[c] = 0;
   }

   delete pPlace;

   cout << "Complete!" << endl;
   return 0;
}

生成文件:

CXX= g++ -g -O3 -fPIC -rdynamic -Wall -Wno-deprecated -DDEBUG
CPP= g++ -g -O3 -fPIC -rdynamic -Wall -Wno-deprecated -DDEBUG

OBJS=   Crash.o
SRCS=   Crash.cpp
TARG=   crash

debug:: ${TARG}

all:: ${TARG}

${TARG}: ${OBJS}
        ${CPP} -o ${TARG} ${OBJS} ${LDFLAGS} ${LIBS}

clean::
        rm -f ${TARG} ${OBJS} ${TARG}.core core

反汇编图(生成的 ASM 代码):

Disassembly graph

最佳答案

看看使用 __attribute__ ((packed)); 而不是 #pragma pack(1)。 IIRC,这个版本的 GCC 对待它有点不同。

关于c++ - `pragma pack(push, 1)` 在 GCC 4.4.7 中崩溃。可能的编译器错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31664452/

相关文章:

c++ - 在 C++ 中读取顺序文件时出错

c++ - 在C++中使用#pragma别名

c++ - ld不拿起图书馆

c++ - 微 Controller - 按钮 'holding down' 监听器

c++ - VirtualAlloc 在某些硬盘配置上失败

c++ - 关于代码重用的问题(模板或继承)

c++ - ARM 平台上的嵌入式 Linux 交叉编译 Clion

c - C 的多个条件编译宏

ios - 无法通过 pragma 禁用 LLVM 优化

gcc - 无法暂时禁用 GCC 中的未知编译指示警告