#pragma pack(push,n) – #pragma pack (pop)

The directive #pragma pack is used to change the current packing alignment value on the internal compiler stack. The packing alignment affects how data is being stored inside a structure, union, or a class.

In order to show the effect of the pack pragma, let’s consider the following code:

#include <iostream>
#include <type_traits>

struct A
{
    char c;
    int n;
    double d;
};

#pragma pack(push,1)

struct B
{
    char c;
    int n;
    double d;
};

#pragma pack(pop)

int main()
{
    A foo;
    B bar;

    // Let's store the same data for "foo" and "bar"
    foo.c = 'A';
    foo.n = 0x12345678;
    foo.d = 0.89898989;

    bar.c = 'A';
    bar.n = 0x12345678;
    bar.d = 0.89898989;

    std::cout << "alignment_of<A>: " << std::alignment_of<A>::value << std::endl;
    std::cout << "alignment_of<B>: " << std::alignment_of<B>::value << std::endl;

    std::cout << "sizeof(A): " << sizeof(foo) << std::endl;
    std::cout << "sizeof(B): " << sizeof(bar) << std::endl;

    // code to print data stored in "foo" and "bar" structures
}

Which is the effect of the directives #pragma pack(push,1) on the structure of type “B”? Which is the effect of #pragma pack(pop)?

As explained in the documentation here, #pragma can be used to push or to pop the current packing alignment value on the internal compiler stack.

In the example above, the compiler uses the one byte packing for B. A different value n can be chosen too. The permitted values for n are: 1, 2, 4, 8 and 16.

If this directive is not specified, then the compiler builds the code using the default packing alignment.

If the explanation is clear, which is then the expected output of the code in the example above?

Please, find the answer here below:

Unlock now all the premium content in this page. Sign-in or register to buy access to this content using your virtual coins.

0 persons like you have already done it. Do not miss it!

If the memory data values are printed for both “foo” and “bar”, which values are expected to be printed?

Please, find the console output here below:

Unlock now all the premium content in this page. Sign-in or register to buy access to this content using your virtual coins.

0 persons like you have already done it. Do not miss it!

Pro and cons

The usage of the #pragma pack directive reduces the amount of memory used to store data inside structures, union, …

READ ALSO  C++ Lambda functions: mastering this Swiss Army knife

Unfortunately, as side effect, a decrease in performance or also a hardware-generated exception for unaligned access might occur. In order to handle these exceptions, the function SetErrorMode() should be called with the input mode parameter equal to SEM_NOALIGNMENTFAULTEXCEPT.

vote
Article Rating
Sharing is Caring
  •   
  •   
  •   
  •   
  •  
  •  
  •  
  •  
    5
    Shares
  • 5
  •  
  •  
  •  
  •  
  •  
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x