Solved

Why Pragma Pack

Posted on 2004-09-03
5
631 Views
Last Modified: 2011-09-20
Dear Experts,
VC6.0++

This is my first time to meet Pragma Pack in my life.

Say, I've the following structure:
typedef sMyStruct{
  DWORD dw1;
  WORD w;
  char mychar1[10];
  DWORD dw2;
  char mychar2[7];
  BOOL b;
  char flag;
} sMyStruct;

1. Can someone explain the function of Pragma Pack?
2. Which Pragma Pack(2,4,6,8,16,32) should be used in above example and why?
3. If I don't use Pragma Pack, what is the result?
Many many thanks.
Rgds.
Alan
--END
0
Comment
Question by:alanpong
5 Comments
 
LVL 13

Accepted Solution

by:
SteH earned 250 total points
ID: 11972229
In normal compilations you can define the alignment of variables which 8 bytes by default. This means that inside a struct this boundary will come into effect as well; no element will not cross a boundary but start at the next boundary instead. (Elements longer than the boundary size will cross the next obviously but not during the first x bytes.
In your struct that means for the addresses of the elements:
0:  dw1 (4 bytes)
4:  w (2 bytes)
6: padding (2 bytes)
8: mychar1 (10bytes)
18: padding (6 bytes)
24: dw2 (4bytes) // not sure thsi could be put at address 20 as well.
28: padding (4 bytes)
32: mychar2 7 bytes)
39: padding (1 byte)
40: b (4 bytes)
44: flag (1 byte)
45: padding (3 bytes)

As you can see this results in a lot of padding bytes but making access to each member efficient. To check look at &(xxx.dw1), &(xxx.w), etc. Whit pragma pack you can specify to change this alignment for that structure. The lower the number means the lower extra space is used for padding but access to a member might need more memory cycles.

Without pargam pack the projects default is used (normally 8 bytes).
0
 
LVL 22

Expert Comment

by:grg99
ID: 11972492
#pragma pack() exists because sometimes you need your structure to have a certain internal field alignment.

If you want maximum speed, most compilers will align fields on whatever boundaries are the easiest and fasttest for the CPU to access.  This is typically 2-byte alignment on 16-bit processors, 4-byte alignment on 32-bit processors, etc...

But sometimes you really NEED a packed struct, with little or no filler bytes for alignment.  The most common reason is you're reading or writing this struct to some other program or module that expects a packed structure.  Or you're short on memory and need the smallest struct possible.

Typically it's best to write code that doesnt depend on the packing strategy.  If you are careful to reference fields individually, and use sizeof(field) as needed, it's usually possible to write code that behaves the same, no matter what the #pack setting.  On the other hand, if you do raw byte moves between fields, or typecast fields to different variable sizes, or blindly step pointers from field to field, #pack can make a huge difference in the program results.
0
 
LVL 2

Expert Comment

by:guntherothk
ID: 11973608
The "best" structure member alignment varies by the architecture of the processor and has gotten bigger over time as CPUs get wider and memory gets cheaper.

You need to control struct member alignment if you are reading or writing a structure to disk or via the network, and you're using write(handle, myStruct, sizeof(myStruct)) to write the whole structure rather than doing it one field at a time, and you want the result to be compatible with some previous machine's output or you're reading some legacy file.

Structure alignment is only one problem when reading/writing legacy files. Byte order is another, and floating point format is a third. So if you're doing legacy file I/O watch out.
0
 
LVL 19

Expert Comment

by:drichards
ID: 11983015
>> Typically it's best to write code that doesnt depend on the packing strategy

I just want to emphasize this point.  Using packed structs to do data IO (network, files, etc.) will only get you in trouble.  Sizes, byte order, data representation (boolean and float values come to mind here) and default alignment cannot be depended on.  You should write code to explicitly handle all these things.

Also, if you use #pragma pack and forget to return it to defaults appropriately, you may really mess things up.  Unless you have a really good reason (there aren't many), it's best not to use #pragma pack.
0
 
LVL 1

Author Comment

by:alanpong
ID: 12023842
Steh, thanks for the concept of padding and the detailed example.

grg99, thanks for the point about moving raw data in struct and typecasting the struct.

guntherothk and drichards, i'll transfer the struct through the network and thanks for the remind.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now