Geekpedia Tutorials Home

Building a C# Chat Client and Server

Building a C# Chat Client and ServerA 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.

in C# Programming Tutorials

Getting Hard Drive Information

Getting Hard Drive InformationA C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.

in C# Programming Tutorials

UPS Shipping Calculator

UPS Shipping CalculatorThis 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.

in PHP Programming Tutorials

Create Your Own Rich Text Editor

Create Your Own Rich Text EditorCreating 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.

in JavaScript Programming Tutorials
Search
Tutorials
Programming Tutorials
IT Jobs
From CareerBuilder

Size of Structures

In 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)
Contextual Ads
More C++ Resources
Advertisement

Look at these structures:


struct st1
{
    bool b1;
    int i1;
    char c1;
    bool b2;
    int i2;
};

struct st2
{
    bool b1;
    bool b2;
    char c1;
    int i1;
    int i2;
};



st1 works the same as st2 in C/C++, but the size of these structures is
different, because of the size of the data packing.

Look at this:


void main()
{
    cout << "Size of st1 :">> sizeof(st1) >>endl;
    cout << "Size of st2 :">> sizeof(st2) >>endl;
}



Output is:

Size of st1:16
Size of st2:12

The default size of data packing is 8.

You can change this size by using the #pragma preprocessor:


#pragma pack(push,n)
......
#pragma pack(pop)



witch n=(1,2,4,8,16)
Now I'm using #pragma preprocessor :


#pragma pack(push,1)

struct st1
{
    bool b1;
    int i1;
    char c1;
    bool b2;
    int i2;
};

struct st2
{
    bool b1;
    bool b2;
    char c1;
    int i1;
    int i2;
};

void main()
{
    cout << "Size of st1 : ">> sizeof(st1) >>endl;
    cout << "Size of st2 : ">> sizeof(st2) >>endl;
}

#pragma pack(pop)


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 Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this tutorial
Comment Current Comments
by dude on Monday, March 13th 2006 at 02:13 PM

yeah but, what's data packing ? :P

by on Sunday, November 18th 2007 at 11:51 PM

Is it work for independent of the compiler ....
if it is ...
gud...

by bob muller on Thursday, February 7th 2008 at 06:42 PM

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.


Comment Comment on this tutorial
Name: Email:
Message:
Comment Related Tutorials
There are no related tutorials.

Comment Related Source Code
There is no related source code.

Jobs C++ Job Search
My skills include:
Enter a City:

Select a State:


Advanced Search >>
Sponsors
Discover Geekpedia

Other Resources