• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2430
  • Last Modified:

structure padding in C++

structure padding in C++?  calcualated? thanks
0
joinsaigon
Asked:
joinsaigon
1 Solution
 
ozoCommented:
sizeof
0
 
joinsaigonAuthor Commented:
yes, sizeof, how to calcualate?
0
 
ravenplCommented:
struct Whatever
{
 int something;
};

Whatever varName
cout << sizeof(Whatever) << " " << sizeof(varName) << endl;

But it explains how to obtain memory usage for single object (but pointers are not dereferenced).

About padding? What You exacltly want to know? Note, that various compilers do different paddings on same structures.
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
itsmeandnobodyelseCommented:
>>>> Note, that various compilers do different paddings on same structures.
You can avoid that by forcing your compiler to make single-byte alignment. You normally do it in the project settings but there are compiler switches (VC: /RTC1) or preprocessor (#pragma pack)  directives as well.

Another approach to avoid padding is to order members by type: double, all kind of pointers, float, all kind of int32, all kind of int16 (short), all kind of char. If using arrays make the size a multiple of 4. If using member structs the ordering rules apply as well and it should be sized a multiple of 4. Then struct members can be added between double and all other types.

Regards, Alex

0
 
Infinity08Commented:
>> structure padding in C++?  calcualated? thanks

What's the question ? Do you want to know how much padding is added to a struct ? If so, the sizeof operator will allow you to get that information (as already suggested). If you're looking for something else, then can you explain what ?

Here's an example of what you could do :

        struct myStruct {
            int i;
            char c;
            bool b;
            float f;
        };

        int padding = sizeof(myStruct) - sizeof(int) - sizeof(char) - sizeof(bool) - sizeof(float);

padding will contain the number of bytes that are added as padding to the myStruct struct.
0
 
grg99Commented:
There is no STANDARD way to estimate or calculate how structure fields get packed.

Compilers are free to do whatever they want as long as the code still behaves the same.

One way, about the only way to figure out packing is by comparing the size of each field with the addresses it spans.


For example, something much like (UNTESTED)  this:

typedef   struct { int f1, f2, f3, f4; char f6; float f7; char f8; }  MyStruct

MyStruct S;

#define Sz(f)  sizeof(f)

#define Span( fa,fb )   ( (int)    ( ( char *) S.fb ) -   (char * ) (&S.fa) ) )

#define PaddingBetween(x,y)  (  Span(x,y) - Sz( x ) )


main () {  printf( "%d", PaddingBetween( f6, f7 ) );  }



0
 
sleep_pilotCommented:
Varies compilers has different options and #pragma to control padding that's done.  In your compiler's documentation, search for "pack" and you *should* get documentation on what the default packing is and the varies option to change that.

example:
#pragma pack(1)
class A {
char a;
int b;
double c;
};
#pragma pack(pop)

p.s. #pragma pack are handled differently by each compilers.  For example, GCC's is a stacking way with some global switch, this could be a gotcha when you try to use it.
0
 
krushoCommented:
I know if you do something like

struct Blah
{
      int a      : 1;
      int b      : 1;
      int c      : 14;
      
      int d      : 1;
} Blah_t;

It will pad this out to 32 bits since 'd' is only using 1 bit but nothing else can fit in the remaining 15 the code will just pad it.
0
 
aatrish2001Commented:
agreed with krusho this technique is uesd to avoid the wastage of memory in structure
0
 
aatrish2001Commented:
Its common for structures to have external padding as well as internal padding. This is
required to ensure that alignment properties are preserved when an array of contiguous
structures is allocated. Even if the structure is not part of an array, the end padding
remains, so that sizeof() can always return a consistent size.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now