473,729 Members | 2,243 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why #pragma pack not take effect?

Struct definition as following(on 32-bit Linux):

#pragma pack(push, 8)
struct MY_STRUCT
{
char a[2];
short b;
short c;
short d;
int e;
long long x;
long long y;
};
#pragma(pop)

During the test, result of 'sizeof(struct MY_STRUCT)' is 28. Why not 32?
As I had expected, a,b,c,d will be packed into one 8-byte, e one and x, y
two. Ain't I right?

If I get wrong usage of #pragma pack(), could anyone please tell me how to
get it work correctly?

BTW, what on earth is the difference between __attribute__ align() and
#pragma pack()?

Thanks in advance!
Jul 3 '07 #1
3 4762
On Jul 3, 3:14 pm, "Jimmy" <lofe...@yahoo. com.cnwrote:
Struct definition as following(on 32-bit Linux):
During the test, result of 'sizeof(struct MY_STRUCT)' is 28. Why not 32?
As I had expected, a,b,c,d will be packed into one 8-byte, e one and x, y
two. Ain't I right?
This is OT here but anyway... assuming this is gcc then 28 bytes is
correct, I don't know why you'd expect otherwise. Note that for x86
gcc usually defines:
char: 1 byte
short: 2 bytes
long: 4 bytes
int: 4 bytes
long long: 8 bytes

Therefore, your struct is:

struct MY_STRUCT
{
char a[2]; /* 2 */
short b; /* 2 */
short c; /* 2 */
short d; /* 2 */
int e; /* 4 */
long long x; /* 8 */
long long y; /* 8 */
/* total: 28 */
};

YMMV since gcc can be modified and recompiled to have different sizes
for variables. But for the sake of being able to correctly compile the
Linux TCP/IP stack this is the most common configuration on most 32bit
(and even most newer 64bit) machines.

Jul 3 '07 #2
Jimmy wrote:
>
.... snip ...
>
BTW, what on earth is the difference between __attribute__ align()
and #pragma pack()?
None. Neither exist in standard C. Find a suitable newsgroup.

--
<http://www.cs.auckland .ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfoc us.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

Jul 3 '07 #3
In article <46************ ***@yahoo.com>,
CBFalconer <cb********@mai neline.netwrote :
>Jimmy wrote:
>>
... snip ...
>>
BTW, what on earth is the difference between __attribute__ align()
and #pragma pack()?

None. Neither exist in standard C. Find a suitable newsgroup.
IOW:
Off topic. Not portable. Cant discuss it here. Blah, blah, blah.

Useful clc-related links:

http://en.wikipedia.org/wiki/Aspergers
http://en.wikipedia.org/wiki/Clique
http://en.wikipedia.org/wiki/C_programming_language

Jul 4 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
5527
by: JustSomeGuy | last post by:
I need to define a structure that is byte packed. I was planning on using #pragma pack (1) So when does the pragma pack end right after the first structure definition or at the end of the header file or until a new pragma pack is encountered... This need to work for visual c++ as well as g++ (gnu cpp 3.3)
10
2658
by: Steven T. Hatton | last post by:
Stroustrup says this: http://www.research.att.com/~bs/bs_faq2.html#macro "So, what's wrong with using macros?" "And yes, I do know that there are things known as macros that doesn't suffer the problems of C/C++ preprocessor macros. However, I have no
6
3964
by: Shri | last post by:
Can anybody tell me where i can find a detailed document on #pragma .... --shri
1
3081
by: Gustavo L. Fabro | last post by:
Greetings! Going directly to the point: myclass.h: //-------------------------------------- #pragma managed //Forward declaration
1
1544
by: Mike Margerum | last post by:
I seem to be running into an issue where I am deriving a new class from a struct with a different alignemnt via the pragma pack #pragma pack(1) typedef struct { unsigned short a; unsigned int b; } MyBase;
15
3751
by: muttaa | last post by:
Hello all, I'm a beginner in C...May i like to know the difference between a #pragma and a #define.... Also,yet i'm unclear what a pragma is all about as i can find topics on it only in high-standard books...
5
3603
by: venkat | last post by:
Hi, I have come across the a pragma definition " #pragma switch direct ". I don't know any thing about pragma's. Even i when i search through google not getting exact information. what does pragma does?. what the statment "#pragma switch direct" does?. Thanks, Venkat.
6
2417
by: Boltar | last post by:
If I use #pragma pack to byte align structures to non word size , will this cause the program to run slightly slower if it is accessing and setting non word aligned integers etc in the struct? Is there any realignment going on silently underneath when the var is read and written to memory? B2003
4
15446
by: Steven Woody | last post by:
In our project, one used the following struct #pragma pack(1) struct Foo { uint8_t b1; uint8_t b2; uint8_t b3; std::vector<uint8_t buf; ... };
0
8913
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9426
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9280
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9142
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8144
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6722
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4795
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2677
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2162
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.