A step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
This tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.
Creating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.
Size of StructuresIn this programming tutorial we will see that 2 structures with the same elements but with different arrange of elements make a different size of structure. |
On Monday, June 14th 2004 at 12:55 AM By Hamed Zaghaghi (View Profile) ![]() ![]() ![]() ![]() (Rated 3.5 with 11 votes) |
||
|
Look at these structures:
Output is: Size of st1:11 Size of st2:11 Note: Don't forget to use #pragma pack(pop) with each #pragma pack(push,n). have a good day. |
|||
Digg It!
Del.icio.us
Reddit
StumbleIt
Newsvine
Furl
BlinkList
|
|||
|
|||
Current Commentsyeah but, what's data packing ? :P
Is it work for independent of the compiler ....
if it is ...
gud...
This tutorial saved me a great deal of time and effort, but it is worth addressing the other two comments. First, in this context, data packing concerns the spacing between items in a structure. The examples given are 16 and 12 bytes long even though the number of stored bytes is only 11. By use of the pragma, the data are stored in an 11 byte structure instead. In my case, learning how to use the #pragma pack(push,n) and pragma pack(pop) pair solved the following problem: on disk, a repeated data item occupied 1828 bytes but a structure written to allow symbolic names to extract elements of the structure occupied 1832 bytes. Consequently, when I tried to get out items after the first couple the wrong values were returned. Why? The data and the structure were misaligned because of compiler details - the compiler ordinarily wants all items aligned to 4 byte boundaries so it caused there to be two 2 byte gaps and thereby caused the problem. By using #pragma pack(push,2) and #pragma pack(pop) to surround the structure the problem went away! No need to write a special reading function to slightly expand the 1828 byte long items into properly aligned 1832 byte structures (or other, even less elegant solutions).
For the second person, these pragmas (and most others) are unfortunately compiler dependent - these work for gcc 4.0 on my Intel based Macintosh but would not unless the compiler instructions tell you they\'re implemented. Note, however, that trying them out can\'t hurt - each compiler presumably ignores unknown pragmas.
Related Tutorials
Related Source Code
C++ Job Search