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! 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.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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)
|
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
|
by: Shri |
last post by:
Can anybody tell me where i can find a detailed document on #pragma ....
--shri
|
by: Gustavo L. Fabro |
last post by:
Greetings!
Going directly to the point:
myclass.h:
//--------------------------------------
#pragma managed
//Forward declaration
|
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;
| |
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...
|
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.
|
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
|
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;
...
};
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |