c++ - object won't pack data -


i'm trying pack data in c++ struct.

my struct has layout:

struct structname {   int16_t member1;   int32_t member2;   uint32_t member3;   uint32_t member4;   uint32_t member5;   etc }__attribute__((packed)); 

using offsetof($structname, $membername) correct offsets of data (0,2,6,10,14 . . .), when access data member-name data @ 4 byte offsets (0,4,8,12,16 . . .) if struct wasn't packed.

is

} __attribute__((packed)); 

the correct way make struct packed? . .

update: mydogisbox wrote:

for record, __attribute__((packed)), #pramga pack(1) , #pragma pack(push, 1) worked.

__attribute__((packed)) gcc extension, supported.

the clang documentation says supports #pragma pack(...) directive:

clang has experimental support extensions microsoft visual c++; enable it, use -fms-extensions command-line option. default windows targets. note support incomplete; enabling microsoft extensions silently drop constructs (including __declspec , microsoft-style asm statements).

clang supports microsoft #pragma pack feature controlling record layout.

source: http://clang.llvm.org/docs/usersmanual.html

just say:

#pragma pack(1) struct my_struct {     int16_t x;     // etc. }; 

to see if works (compile -fms-extensions if not using windows).

note above non-standard extensions, , new c++11 standard has new alignas keyword: http://en.cppreference.com/w/cpp/language/alignas

struct alignas(1) my_struct {     int16_t x;     // etc. }; 

but support still bit sketchy.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -