pragma-pack

pragma pack(push) without corresponding pop leads to stack smashing

好久不见. 提交于 2021-01-28 12:26:21
问题 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 the exact scenario and code are as follows. I was following a C++ course and had written a code for the project. My program was crashing due to stack smashing. I tried to look for any obvious overflow errors but couldn't find any. I changed almost

what is the difference between __attribute__((__packed__)); and #pragma pack(1)

你说的曾经没有我的故事 提交于 2020-07-03 03:56:06
问题 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 error on windows: '__packed__' : undeclared identifier I am wondering if I can fix this error by using #pragma pack(1) is there any difference between them? Is there any syntax that can be used in Linux and Windows for this attribute? 回答1: __attribute__ is a GCC extension

Is gcc's __attribute__((packed)) / #pragma pack unsafe?

孤人 提交于 2019-11-26 03:13:46
问题 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 extension, __attribute__((packed)) , which tells the compiler not to insert padding, allowing struct members to be misaligned. For example, if the system normally requires all int objects to have 4-byte alignment, __attribute__((packed)) can cause int

#pragma pack effect

佐手、 提交于 2019-11-25 22:38:59
问题 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 with experience. I\'ve seen it in code before, though I can\'t seem to find where anymore. 回答1: #pragma pack instructs the compiler to pack structure members with particular alignment. Most compilers, when you declare a struct, will insert padding