Packing class or struct to place its members directly after each other in memory, which can mean that some or all members can be aligned on a boundary smaller than the default alignment the target architecture
Questions tagged [pragma-pack]
29 questions
294
votes
11 answers
#pragma pack effect
I was wondering if someone could explain to me what the #pragma pack preprocessor statement does, and more importantly, why one would want to use it.
I checked out the MSDN page, which offered some insight, but I was hoping to hear more from people…

Cenoc
- 11,172
- 21
- 58
- 92
202
votes
5 answers
Is gcc's __attribute__((packed)) / #pragma pack unsafe?
In C, the compiler will lay out members of a struct in the order in which they're declared, with possible padding bytes inserted between members, or after the last member, to ensure that each member is aligned properly.
gcc provides a language…

Keith Thompson
- 254,901
- 44
- 429
- 631
9
votes
3 answers
Scope of the pragma pack directive in Visual Studio
What is the scope of the #pragma pack alignment in Visual C++? The API reference
https://msdn.microsoft.com/en-us/library/vstudio/2e70t5y1%28v=vs.120%29.aspx
says:
pack takes effect at the first struct, union, or class declaration
after the…

rgb
- 1,750
- 1
- 20
- 35
8
votes
1 answer
C++ #pragma pack
What does the following statement actually do and what are it's effects?
#pragma pack(push,8)

Tony The Lion
- 61,704
- 67
- 242
- 415
6
votes
1 answer
what is the difference between __attribute__((__packed__)); and #pragma pack(1)
I am porting a code which runs perfectly on Linux to windows visual c++.
I have this code in Linux:
struct exif_desc
{
uint16_t tag;
uint16_t type;
uint32_t length;
uint32_t value;
}
__attribute__((__packed__));
I am getting…

mans
- 17,104
- 45
- 172
- 321
3
votes
1 answer
How should #pragma pack(8) work?
I'm new to structure alignment and packing. I thought I understood it, but I'm finding some results that I did not expect (see below).
My understanding of structure alignment is:
Types are generally aligned on memory addresses that are multiples of…

jne
- 33
- 1
- 3
2
votes
2 answers
Why #pragma pack also affects structs' own alignment?
I noticed that when #pragma pack is used around a struct, the alignment inside of it is not the only one that is affected, but also the alignment of the struct itself changes. Consider the following:
#include
#include
#pragma…

ZenJ
- 313
- 3
- 15
2
votes
1 answer
pragma pack(push) without corresponding pop leads to stack smashing
I used #pragma pack(push, 2) at the beginning of a struct in a header file but forgot the corresponding #pragma pack(pop). After including this header file, I included fstream. On creating an ofstream object, I am seeing stack smashing. Details of…

vkj
- 43
- 3
1
vote
1 answer
pragma pack(push) on iphone
I have a structure within #pragma pack(push,1) and #pragma pack(pop). I would like to know if this will properly work on iphone compiler, and if there might be other side effects outside of those macros.
Thanks,
Raxvan.

Raxvan
- 6,257
- 2
- 25
- 46
1
vote
1 answer
Struct alignment is always 4
Why pragma pack does not work in here? sizeof() returning the size aligned to 4. I use compiler which comes with Microsoft Visual Studio 2015
#pragma pack(push,1)
struct Header {
uint16_t zero = 0;
uint32_t id{};
};
struct ImageBlock :…

Valerii Chistyakov
- 41
- 4
1
vote
0 answers
I don't understand this mistake about #pragma
This is the first part of my code and I can't understand the mistake written at the end of the message. Maybe I don't know enough to understand it. I would be very grateful if someone can explain me how #pragma works and what means the mistake.…

Ernestina Lede
- 11
- 1
1
vote
0 answers
What is the alternative to using #pragma pack (push n) and #pragma pack (pop)
I've been reading a lot of comments on the use of #pragma pack, nearly all negative.
I understand what it does and I can see the negative effects it may have but is there an alternative?
Most of the code I write is to read/write/manipulate GIS data.…

Colins2
- 11
- 3
1
vote
1 answer
Nim: How to wrap a c struct that contains bitfields?
The question is if I see a bitfield in a c struct as such:
struct Example
{
int A;
int B;
int C : 3;
int D : 3;
int E : 3;
int F : 8;
bool G : 1;
bool H : 1;
};
Would the correct binding in Nim be as such?
type…

Joshua Fenner
- 355
- 1
- 7
1
vote
1 answer
Problem in converting Packed C structs to go structure by cgo
When we use #pragma pack (push, 1) to pack the C structure and convert to go structure by cgo, some of the fields are missing in the converted Go structure.
I am using Go version: 1.16.6 Windows/386
For example, my C struct is like this:
#pragma…

Priya Purushoth
- 38
- 6
1
vote
0 answers
Influence of #pragma pack on outer struct?
Problem
I stumbled over alignment issues when accessing a member of a struct (on ARMv7). The accessed member itself is also a struct but packed with the infamous #pragma pack(1) preprocessor directive. The outer struct is not packed so a naive…

MarcusS
- 176
- 2
- 10