#pragma pack()

I have just spent the last hour bashing my head, trying to workout why some new C++ code (to my project) was crashing.

I was constructing one class, and it was building some worker classes and crashing in the std::vector code when the worker class was being destroyed.

It just wasn’t making sense.

Why would code that’s been running for another team for over a year, blow-up when I use it?

I turned to a co-worker, described what was happening and said “it’s like some memory corruption or packing issue

And it was a structure packing alignment issue. The imported project had default packing (8), and my really old solution uses packing (1), so somewhere the default class functions where getting generated with different opinions from where member’s were….

#pragma pack(push)
#pragma pack(4)
#pragma pack(pop)

to the rescue, and low and behold the problems gone. I hate this type of issue. Importing existing C++ projects happens so infrequently that this issue is not on the must check list. Where-as I was convinced I had fluffed up the usage of the new code….

1 thought on “#pragma pack()”

Comments are closed.