Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Question about using #pragma pack

Reply
Thread Tools

Question about using #pragma pack

 
 
Boltar
Guest
Posts: n/a
 
      03-20-2008
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
 
Reply With Quote
 
 
 
 
jacob navia
Guest
Posts: n/a
 
      03-20-2008
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
 
Reply With Quote
 
 
 
 
Richard Tobin
Guest
Posts: n/a
 
      03-20-2008
In article <(E-Mail Removed)>,
Boltar <(E-Mail Removed)> 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 probably. Otherwise why would they bother to align them in the
usual case?

-- Richard
--
:wq
 
Reply With Quote
 
Boltar
Guest
Posts: n/a
 
      03-20-2008
On Mar 20, 11:39 am, jacob navia <(E-Mail Removed)> wrote:
> 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

 
Reply With Quote
 
jacob navia
Guest
Posts: n/a
 
      03-20-2008
Boltar wrote:
> On Mar 20, 11:39 am, jacob navia <(E-Mail Removed)> wrote:
>> 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
 
Reply With Quote
 
Richard Tobin
Guest
Posts: n/a
 
      03-20-2008
In article <(E-Mail Removed)>,
Boltar <(E-Mail Removed)> wrote:
>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
 
Reply With Quote
 
Kenneth Brody
Guest
Posts: n/a
 
      03-20-2008
Boltar wrote:
>
> On Mar 20, 11:39 am, jacob navia <(E-Mail Removed)> wrote:
> > 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: <(E-Mail Removed)>

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      03-20-2008
Boltar <(E-Mail Removed)> writes:
> 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?


"#pragma pack" is a non-standard extension. If you want to know how
it works, you'll need to consult the documentation for your
implementation; different compilers might implement it differently, or
not at all.

--
Keith Thompson (The_Other_Keith) <(E-Mail Removed)>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
When Is A Visaster Service Pack Not A Service Pack? Lawrence D'Oliveiro NZ Computing 5 06-26-2008 09:11 PM
Buy 1 pack or 3 pack? (XP Pro x64) Rich Olver Windows 64bit 11 11-25-2006 11:33 PM
Excellent discount software packs - ImTOO Ripper Pack Platinum and ImTOO Ripper Pack Gold zhangelf01@gmail.com DVD Video 6 09-17-2006 03:27 AM
Pack parent control in child using control.pack(in_= syntax? Tim Jones Python 0 01-31-2004 10:22 PM
Poor Mans NIMH Battery Pack and SLA Battery Pack ajacobs2 Digital Photography 0 08-19-2003 12:42 PM



Advertisments