473,757 Members | 9,145 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question about using #pragma pack

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
Mar 20 '08 #1
6 2418
Boltar wrote:
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?
yes

Is there any
realignment going on silently underneath when the var is read and
written to memory?
no

B2003

--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
Mar 20 '08 #2
In article <9e************ *************** *******@c65g200 0hsa.googlegrou ps.com>,
Boltar <bo********@yah oo.co.ukwrote:
>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?
Yes probably. Otherwise why would they bother to align them in the
usual case?

-- Richard
--
:wq
Mar 20 '08 #3
On Mar 20, 11:39 am, jacob navia <ja...@nospam.c omwrote:
Boltar wrote:
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?

yes

Is there any
realignment going on silently underneath when the var is read and
written to memory?

no
I presume it must use 2 seperate get/put operations for a variable
crossing a word boundary then?

B2003

Mar 20 '08 #4
Boltar wrote:
On Mar 20, 11:39 am, jacob navia <ja...@nospam.c omwrote:
>Boltar wrote:
>>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?
yes

Is there any
>>realignment going on silently underneath when the var is read and
written to memory?
no

I presume it must use 2 seperate get/put operations for a variable
crossing a word boundary then?

B2003
This depends on the processor
In some cases the alignment fault will be immediately trapped by the
processor that then issues two reads, or it could be that it is trapped
by the operating system, what would be MUCH MORE costly.

At each access you would pay the cost of an interupt servicing, and then
OS support.

Conclusion:

Do not use anything and leave the stuff to the compiler.
--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
Mar 20 '08 #5
In article <94************ *************** *******@c19g200 0prf.googlegrou ps.com>,
Boltar <bo********@yah oo.co.ukwrote:
>I presume it must use 2 seperate get/put operations for a variable
crossing a word boundary then?
It may be that the hardware or microcode does this for you (it does on
x86 for example), but it's still likely to be slower.

It might be that within a cache line it would be possible to do
unaligned reads and writes with no overhead, but I don't think that's
the case. Try comp.arch which has people who really understand this.

-- Richard
--
:wq
Mar 20 '08 #6
Boltar wrote:
>
On Mar 20, 11:39 am, jacob navia <ja...@nospam.c omwrote:
Boltar wrote:
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?
yes

Is there any
realignment going on silently underneath when the var is read and
written to memory?
no

I presume it must use 2 seperate get/put operations for a variable
crossing a word boundary then?
"Best case scenario", probably.

However, there are other possibilities. Some hardware simply doesn't
allow such access, and causes an interrupt. On some platforms, this
will cause the program to crash. On others, the O/S will emulate the
unaligned access ability in the interrupt handler. For example, on
an unaligned read, it will read the two aligned chunks, pull out the
bits that you wanted, put then in the destination, and return from
the interrupt handler. (Hardly a simple "2 bus reads" degradation
in efficiency.)

You may want to look back at why you feel you need to pack the
structs.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer .h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th***** ********@gmail. com>

Mar 20 '08 #7

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

Similar topics

18
2448
by: Peter Smithson | last post by:
Hi, I've read this page - http://devrsrc1.external.hp.com/STK/impacts/i634.html but don't understand it. Here's the text - "Non-standard usage of setjmp() and longjmp() could result in compatibility problems. The contents of the jmp_buf buffer are specific
4
1621
by: xudeutsch | last post by:
thanks, Nicholas and cody. but as far as i know, the following directive " #pragma pack(push,4)" will ensure that: when the data are transferred through networks to other platforms, the const value "STATE_NULL" will still be regarded as a 4-byte value in the stack, namely 0x0000. I dont know whether other statements are needed or it is default that on every machine this hex value will be treated as 4-byte. Is that not in 2-byte?
9
2385
by: Giovanni Bajo | last post by:
Hello, with Visual Studio .NET 2003: __declspec(align(1)) struct X { unsigned int x : 3; unsigned int y : 13; };
1
1546
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;
28
3641
by: kyle york | last post by:
Greetings, Why does the C standard require the members of a structure not be re-ordered (6.2.5.20)? Padding is allowed, and platform dependent, which means one cannot rely on the exact layout anyway, so what's the point? Without this restriction the compiler could layout the structure in the most efficient way possible, for some definition of efficient. It would be easy enough to turn this reordering off with a compiler specific...
10
7488
by: =?Utf-8?B?TmFuZCBLaXNob3JlIEd1cHRh?= | last post by:
I have a binary file created using C++ that contains an object of the structure: struct student { int roll_no; char name; char qualification; };
3
4765
by: Jimmy | last post by:
Struct definition as following(on 32-bit Linux): #pragma pack(push, 8) struct MY_STRUCT { char a; short b; short c; short d; int e;
2
3351
by: calenlas | last post by:
Hi all, I'm taking my first steps into C# <--C++ DLL Interop and unfortunately I've run into (what seems to be) a very complicated case as my first task. Perhaps someone here can help me. I need to pass an array of RADIO_INFO2 structures to be filled by a function in the DLL. This is how the structure is defined in the C++ example that comes with the DLL:
20
2044
by: xiao | last post by:
Hi~ every one~ I have a queston about fread function. if i have a code like this: (nscrdh and data are defined as two dementional arrays and both of them were stored in the same binary file) fread(&nscrdh,sizeof(nscrdh),1,in); fread(&data,sizeof(data),1,in); How can i know where the second fread start? Is it just start from the end of the first fread? Or somewhere else?
0
10072
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...
1
9885
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8737
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...
0
6562
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5172
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3829
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3399
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2698
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.