Click here to Skip to main content
15,897,371 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
Generalerror Pin
dudic5-Jul-03 23:39
dudic5-Jul-03 23:39 
GeneralRe: error Pin
Ryan Binns5-Jul-03 23:55
Ryan Binns5-Jul-03 23:55 
GeneralRe: error Pin
Michael Dunn6-Jul-03 2:59
sitebuilderMichael Dunn6-Jul-03 2:59 
GeneralQuestion about #pragma pack(1) Pin
George25-Jul-03 21:46
George25-Jul-03 21:46 
GeneralRe: Question about #pragma pack(1) Pin
Rick York5-Jul-03 22:03
mveRick York5-Jul-03 22:03 
GeneralRe: Question about #pragma pack(1) Pin
George25-Jul-03 22:12
George25-Jul-03 22:12 
GeneralRe: Question about #pragma pack(1) Pin
peterchen5-Jul-03 22:18
peterchen5-Jul-03 22:18 
GeneralRe: Question about #pragma pack(1) Pin
peterchen5-Jul-03 22:16
peterchen5-Jul-03 22:16 
todays CPU's are much faster when they read a 4-byte value from a multiple-of four address, 2-byte values from a multiple-of-two adress, etc.

Certain processor architectures don't even allow to read a value from a misaligned address. On on x86/Pentium, e.g. reading a long from address 0x10001, 0x10002, or 0x10003(which is are not multiples of 4) causes two memory reads and extra overhead.

Compilers adds "fill" bytes to structs, classes and array members members so that it's members ae "well aligned" (it assumes that the beginning of the struct is well aligned). The "maximum padding" done is usually controlled by compiler options, like #pragma pack.

#pragma pack(1) indicates to the VC++ compiler that it should align following struct declarations to full byte adresses (i.e. no padding noe *). e.g. for the
struct foo      
{               // default (align=8)      pack(1)
  char   c;     // & = this+0             & = this + 0
                // (3 pad bytes)          (no pad bytes)   
  int    count; // & = this+4             & = this + 1
};
´

Usually you would leave the default pakcing of 8, unless:
a) you have to simulate a certain binary structure - like a binary file header
b) you are very tight with memory and don't care about speed (and have a processor architecture that allows misaligned memory access)
c) you want to organize padding yourself

VC offers #pragma pack(push, <value>) to save the current csetting and #pragma pack(pop) to restore the previous setting. This is advised to use everywhere, since alignment mismatch isn't detected by the linker. (Assume above struct is #included once with pad byte, and once without).

When deciding the memory layout of a struct or class, the C++ standard only guarantees that elements go in memory in the source code order, but how much padding is added is up to the compiler.

Not as brief as you expected, but the basic briefing...

*) There's an exceptionm - bitfields




"Der Geist des Kriegers ist erwacht / Ich hab die Macht" StS

sighist | Agile Programming | doxygen

GeneralRe: Question about #pragma pack(1) Pin
George26-Jul-03 17:10
George26-Jul-03 17:10 
GeneralRe: Question about #pragma pack(1) Pin
peterchen6-Jul-03 21:36
peterchen6-Jul-03 21:36 
GeneralRe: Question about #pragma pack(1) Pin
George26-Jul-03 21:57
George26-Jul-03 21:57 
GeneralRe: Question about #pragma pack(1) Pin
peterchen6-Jul-03 22:26
peterchen6-Jul-03 22:26 
GeneralRe: Question about #pragma pack(1) Pin
George26-Jul-03 22:51
George26-Jul-03 22:51 
GeneralRe: Question about #pragma pack(1) Pin
peterchen7-Jul-03 1:15
peterchen7-Jul-03 1:15 
GeneralHexadecimal representation Pin
punkms15-Jul-03 20:46
punkms15-Jul-03 20:46 
GeneralRe: Hexadecimal representation Pin
Rick York5-Jul-03 21:31
mveRick York5-Jul-03 21:31 
GeneralRe: Hexadecimal representation Pin
Ryan Binns5-Jul-03 22:05
Ryan Binns5-Jul-03 22:05 
GeneralRe: Hexadecimal representation Pin
peterchen5-Jul-03 22:21
peterchen5-Jul-03 22:21 
GeneralRe: Hexadecimal representation Pin
Ryan Binns5-Jul-03 22:31
Ryan Binns5-Jul-03 22:31 
GeneralRe: Hexadecimal representation Pin
peterchen6-Jul-03 21:41
peterchen6-Jul-03 21:41 
GeneralRe: Hexadecimal representation Pin
Ryan Binns6-Jul-03 22:27
Ryan Binns6-Jul-03 22:27 
GeneralRe: Hexadecimal representation Pin
Rick York5-Jul-03 22:32
mveRick York5-Jul-03 22:32 
GeneralRe: Hexadecimal representation Pin
peterchen5-Jul-03 21:42
peterchen5-Jul-03 21:42 
GeneralRe: Hexadecimal representation Pin
punkms15-Jul-03 22:04
punkms15-Jul-03 22:04 
GeneralRe: Hexadecimal representation Pin
peterchen5-Jul-03 22:32
peterchen5-Jul-03 22:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.