Questions tagged [pragma-pack]

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

29 questions
1
vote
3 answers

What is `#pragma pack` for in network programming?

#pragma pack(push) #pragma pack(1) I downloaded a tutorial and it has these lines in the header file. I will appreciate if you guys can provide me any tutorials or references related to this.
codereviewanskquestions
  • 13,460
  • 29
  • 98
  • 167
1
vote
1 answer

wcslen() returns incorrect result when pragma pack used

I've found that wcslen() returns incorrect result on gcc(and correct on msvc) when source is wchar_t array, member of packed struct. I know on linux sizeof(wchar_t) == 4 and on windows its 2 but still can't understand how packing affects wcslen()…
Alex
  • 13
  • 5
1
vote
2 answers

Copying a struct into a byte array

I have a 1-byte pragma packed struct in C which I want to copy into a byte array for serialization purpose to be sent over a serial port. #pragma pack(push, 1) typedef struct { uint8_t ck_a; uint8_t ck_b; } UBXChecksum_t ; #pragma…
Akay
  • 1,092
  • 12
  • 32
1
vote
1 answer

Compile time check for usage of #pragma pack

Most compiliers support changing the packing of a class by using a #pragma pack(N) directive, where N is the new minimum acceptable alignment for each member. Is it possible to check at compile-time whether or not a #pragma pack(N) has been…
Adam Yaxley
  • 670
  • 1
  • 7
  • 13
1
vote
1 answer

passing address of packed struct member

I am developing a C/C++ application where in I am using packed struct members. I have read that we should never pass address of packed struct members to any function(I always used to get alignment errors when passing packed struct members by…
Harry
  • 2,177
  • 1
  • 19
  • 33
0
votes
1 answer

pragma pack(n) vs pragma pack(push,n)

What is the difference between pragma pack(n) and pragma pack(push,n) #pragma pack(pop) and #pragma pack() Can someone explain through an example ? Which should be used when ? I went through…
Cool Camel
  • 57
  • 6
0
votes
1 answer

What is the effect of #pragma pack() when it follows a struct definition?

I have read other posts as well as the Microsoft Docs regarding the effect of #pragma pack(). I understand that it is useful when byte alignment is necessary. However, I am still unsure of the effect of putting #pragma pack() both before and after…
0
votes
0 answers

Forgot pragma pack(pop) program crashes

#pragma pack(push, 1) typedef enum __attribute__ ((__packed__)) { ETypeZero = 0, ETypeOne, ETypeTwo, ETypeUndefined } ESomeType; #pragma pack(pop) In my code above, I had accidentally forgot #pragma pack(pop), and I saw some other…
Nishikant
  • 67
  • 5
0
votes
1 answer

How to align a structure correctly?

I am trying to align a structure using the directive (#pragma pack). I need it has 112 bytes in size. (14*8=112 bytes). However it has 80 bytes only. How to do it correctly? #pragma pack (8) struct Deal { long deal_ticket; long …
Iván Rodríguez
  • 447
  • 4
  • 10
0
votes
2 answers

C macro for inlining functions with IAR compiler

With previous gcc compiler we did this: #define DO_PRAGMA(x) _Pragma(#x) #define PACK_ON(n) DO_PRAGMA(pack(n)) so in effect would mean PACK_ON(2) would expand to be _Pragma(pack(2)) Then we would use it like this PACK_ON(2) typedef…
Adeishere
  • 41
  • 4
0
votes
1 answer

Byte layout of structure (#pragma pack behavior) different on MSVC vs clang/gcc

The following code generates a different layout in memory on MSVC vs clang/gcc. Why? #include #pragma pack(push,1) struct empty_struct { }; class derived_struct : public empty_struct { int m_derivedMember; }; class…
Phenglei Kai
  • 413
  • 5
  • 14
0
votes
1 answer

#pragma pack(1) causes segmentation fault

At some point my big project's code started getting segmentation fault runtime errors with such stacktrace: 0# std::basic_ios >::widen (__c=10 '\n', this=) at /usr/include/c++/7/bits/basic_ios.h:450 1# std::endl > (__os=...) at…
Slaus
  • 2,086
  • 4
  • 26
  • 41
0
votes
2 answers

Is `#pragma pack(1)` risky/dangerous if a struct contains only byte arrays

Is it dangerous/risky to use #pragma pack(1) on structs that contain byte-arrays only? E.g. this one: #pragma pack(1) struct RpcMessage { uint8_t proto_info[16]; uint8_t message_uuid[16]; uint8_t arg0[16]; uint8_t arg1[16]; …
K. Biermann
  • 1,295
  • 10
  • 22
0
votes
3 answers

C - Why #pragma pack(1) Consider 6-bit struct member as an 8-bit?

I got stuck about #pragma pack(1) wrong behavior when define a 6-bit field and assumes it as 8-bit. I read this question to solving my problem but it doesn't help me at all. In Visual Studio 2012 I defined bellow struct for saving Base64 characters…
1
2