GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 28-May-2004, 03:36
pinkpanther
 
Posts: n/a

strange sizeof(structure) - multiple of 8


Hi all, I'm a bit confused about this:
I'm using a structure containing long ints, ints, doubles, chars and array of chars; however I try to change the number of fileds in this structure (ie. adding one or more ints or chars) when I call the sizeof() of this structure it returns a number larger than the sum of all sizeof() for each field - number which, after hours of struggle, I found to be a multiple of 8 nomatter what!!
So no matter how many fields my structure has, as long as it contains both numbers and chars, the sizeof() the whole thing returns a multiple of 8 .

My question is: why ?
  #2  
Old 28-May-2004, 03:55
pinkpanther
 
Posts: n/a

further details


I know I did not make myself very clear, here's an example:


CPP / C++ / C Code:

#include <stdio.h>

#define ABC_STRUCT struct abc_struct
ABC_STRUCT
{
   char a_char;
   
   int a_num;
};

void main()
{
   ABC_STRUCT  my_struct;
   
   printf("\nSize Of my char : %d\n", (int)sizeof(my_struct.a_char));
   printf("Size Of my int : %d\n", (int)sizeof(my_struct.a_num));
   printf("\nSize Of my structure : %d\n", (int)sizeof(my_struct));
   
   exit(0);
}


Output to console is:

-----------------------
Size Of my char : 1
Size Of my int : 4

Size Of my structure : 8
-----------------------

This thing returns 8 when it should ( sizeof(a_char) + sizeof(a_num) ) that is 5.
I'm using a Digital UNIX V4.0E (Rev. 1091).

thanks.
  #3  
Old 28-May-2004, 08:53
machinated machinated is offline
Regular Member
 
Join Date: Mar 2004
Location: victoria, canada
Posts: 324
machinated has a spectacular aura aboutmachinated has a spectacular aura about
My guess is that the entire structure will take up memory in multiples of the size of its biggest member. if your structure contains 7 chars, it will take up 7 bytes in total. if your structure contains 1 int and 3 chars, it will take up 8 bytes. if your structure contains 1 int and 5 chars, it will take up 12 bytes. Same goes for char*. If it contains 1 double and 1 int, it will take up 16 bytes.

This doesnt apply to structures within structures. for example if you had a struct which is 5 bytes within a struct which has 2 chars, it will take up 7 bytes instead of 10 bytes. Only applies to c primitive data types.

I think it has to do with the way it's stored in memory and/or registers. I will find out why it happens exactly and post it later on.
__________________
spasms!!!
  #4  
Old 28-May-2004, 09:01
pinkpanther
 
Posts: n/a
Thanks, machinated.
However I can tell you that even with 1 int and 5 chars it will not allocate 12 but 16; however the reason must be the one you mentioned.

Thanks again.
  #5  
Old 28-May-2004, 12:55
machinated machinated is offline
Regular Member
 
Join Date: Mar 2004
Location: victoria, canada
Posts: 324
machinated has a spectacular aura aboutmachinated has a spectacular aura about
Quote:
Originally Posted by pinkpanther
Thanks, machinated.
However I can tell you that even with 1 int and 5 chars it will not allocate 12 but 16; however the reason must be the one you mentioned.

Thanks again.

that's weird because when i try it it allocates 12 bytes.
__________________
spasms!!!
  #6  
Old 28-May-2004, 13:03
pinkpanther
 
Posts: n/a
Well it is weird then, cause I've tried it more than once, and it shows sizeof() as 16.. no doubt it must be a compiler thing, but don't bother about it, it's not that important.
Thanks again for your support !
  #7  
Old 28-May-2004, 18:14
WaltP's Avatar
WaltP WaltP is online now
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by pinkpanther
Well it is weird then, cause I've tried it more than once, and it shows sizeof() as 16.. no doubt it must be a compiler thing, but don't bother about it, it's not that important.
Thanks again for your support !
Actually it has nothing to do with the actual contents of your structure. The compiler pads the structure to the nearest 4- or 8-byte boundary because it's easier on the system.

So, if you define a structure with 17 bytes, the system will allocate 24 (if allignment is on 8-byte boundaries and not 4)
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
  #8  
Old 29-May-2004, 02:22
pinkpanther
 
Posts: n/a
Quote:
Originally Posted by WaltP
Actually it has nothing to do with the actual contents of your structure. The compiler pads the structure to the nearest 4- or 8-byte boundary because it's easier on the system.

So, if you define a structure with 17 bytes, the system will allocate 24 (if allignment is on 8-byte boundaries and not 4)


Aha.. good to know; I never read this anywhere, so - thanks alot for clearing it out .

However if I try a structure of 17 chars, either 17 fields of char or a char array[17], the sizeof() returns the right, precise, 17 bytes.
The padding phenomenon only happens when alternating a number type with a char type.

Greetings.
  #9  
Old 29-May-2004, 08:48
machinated machinated is offline
Regular Member
 
Join Date: Mar 2004
Location: victoria, canada
Posts: 324
machinated has a spectacular aura aboutmachinated has a spectacular aura about
yes exactly. if you have 5 chars, ur structure will take up 5 bytes. So it does have to do with the content of your structure.

Each type has an alignment or padding assigned to it in the compiler. char has 1 byte, short has 2, integer has 4, and double has 8 in MSVC. In GCC, double has 4 bytes of alignment (which i think is very smart since a register is 4 bytes). Apparently in your compiler, integer has 8 bytes of alignment which is a waste
__________________
spasms!!!
  #10  
Old 29-May-2004, 20:34
WaltP's Avatar
WaltP WaltP is online now
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by machinated
yes exactly. if you have 5 chars, ur structure will take up 5 bytes. So it does have to do with the content of your structure.

Not exactly. Take this program:
CPP / C++ / C Code:
#include <stdio.h>

typedef struct 
{
    int bb;
    char a[3];
} bb1;

typedef struct 
{
    int bb;
    char a[4];
} bb2;

typedef struct 
{
    int bb;
    char a[5];
} bb3;

typedef struct 
{
    int bb;
    char a[6];
} bb4;

typedef struct 
{
    int bb;
    char a[7];
} bb5;

typedef struct 
{
    int bb;
    char a[8];
} bb6;

typedef struct 
{
    int bb;
    char a[9];
} bb7;

typedef struct 
{
    int bb;
    char a[10];
} bb8;

typedef struct 
{
    int bb;
    char a[11];
} bb9;



int main() 
{
    bb1 bs1;
    bb2 bs2;
    bb3 bs3;
    bb4 bs4;
    bb5 bs5;
    bb6 bs6;
    bb7 bs7;
    bb8 bs8;
    bb9 bs9;

    printf(" %d %d %d %d %d %d %d \n"
            , sizeof(bs1)
            , sizeof(bs2)
            , sizeof(bs3)
            , sizeof(bs4)
            , sizeof(bs5)
            , sizeof(bs6)
            , sizeof(bs7)
            , sizeof(bs8)
            , sizeof(bs9)
            );
}

When I compile it normally, the output I get is 8 8 12 12 12 12 16
When I compile it with the 'packed' switch, it's 7 8 9 10 11 12 13

The packed switch tell the compiler not to pad data to align values on word boundaries. Generally, the compiler will pad data unless told otherwise. This is what's happening to you.
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
 
 

Recent GIDBlogUS Elections and the ?Voter?s Responsibility? by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Cd burner behaves strange Julepessimisten Computer Hardware Forum 0 03-Jan-2004 15:17
Customising SMS sending script via e-mail gateway for multiple users jrobbio MySQL / PHP Forum 4 29-Sep-2003 13:49
multiple Cpanel/WHM servers manager simscripts Web Hosting Forum 0 09-Sep-2003 19:10
I am getting STRANGE emails / feedback today! JdS Learning Journal by J de Silva 4 24-Aug-2003 19:16
SQL multiple languages query samtediou MySQL / PHP Forum 6 23-Jul-2003 14:09

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 02:41.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.