Help Me !! some strange in a structure size 
Author Message
 Help Me !! some strange in a structure size

Hi..everyone..

Hmm.. I have a question for a structure size.
For instance, if I declare a structure following.

struct s {
    int * a;
    int * b;
    unsigned char c;

Quote:
};

Then, the size of structure s is 9 bytes in 32-bit platform, isn't it ?
But, as the result of test, it has 12 bytes.

I had tested it with GNU gcc.

Why don't have it 9 bytes exactly ?

Thanks in advance.



Thu, 29 May 2003 19:56:50 GMT  
 Help Me !! some strange in a structure size

Quote:
>Hi..everyone..

>Hmm.. I have a question for a structure size.
>For instance, if I declare a structure following.

>struct s {
>    int * a;
>    int * b;
>    unsigned char c;
>};

>Then, the size of structure s is 9 bytes in 32-bit platform, isn't it ?
>But, as the result of test, it has 12 bytes.

>I had tested it with GNU gcc.

>Why don't have it 9 bytes exactly ?

This is a FAQ. Never make assumptions about structures size. There are
definitely implementation-dependent:

--
-hs-    Tabs out, spaces in.
CLC-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
ISO-C Library: http://www.dinkum.com/htm_cl
FAQ de FCLC : http://www.isty-info.uvsq.fr/~rumeau/fclc



Thu, 29 May 2003 19:59:20 GMT  
 Help Me !! some strange in a structure size

Quote:
>This is a FAQ. Never make assumptions about structures size. There are
>definitely implementation-dependent:

Oops, posted too fast by my daughter's Guinea Pig...

#include <stdio.h>

struct s
{
   int *a;
   int *b;
   unsigned char c;

Quote:
};

int main (void)
{
   printf ("sizeof struct s = %lu\n"
          ,(unsigned long) sizeof (struct s)
          );

   return 0;

Quote:
}

Borland C with BYTE alignment :

D:\CLC\DEV_S\SUKANG>bc proj.prj
sizeof struct s = 5

Borland C with WORD alignment :

D:\CLC\DEV_S\SUKANG>bc proj.prj
sizeof struct s = 6

DJGPP (gcc for DOS)

D:\CLC\DEV_S\SUKANG>rhide
This is RHIDE Version 1.4. Copyright (c) 1996,1997 by Robert H?hne
             (Sep 30 1997 23:06:59)
sizeof struct s = 12

--
-hs-    Tabs out, spaces in.
CLC-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
ISO-C Library: http://www.dinkum.com/htm_cl
FAQ de FCLC : http://www.isty-info.uvsq.fr/~rumeau/fclc



Thu, 29 May 2003 20:08:34 GMT  
 Help Me !! some strange in a structure size
Field alignment in structure depends on how the compiler aligns the
field within the structure (say, 32-bit alignment / 16 (WORD) alignment)
...

If you want the actual size you may use the pragma directives to *pack*
the structures ...
#pragma pack(1)

Hope this helps.

Sudhagar

Quote:

> Hi..everyone..

> Hmm.. I have a question for a structure size.
> For instance, if I declare a structure following.

> struct s {
>     int * a;
>     int * b;
>     unsigned char c;
> };

> Then, the size of structure s is 9 bytes in 32-bit platform, isn't it ?
> But, as the result of test, it has 12 bytes.

> I had tested it with GNU gcc.

> Why don't have it 9 bytes exactly ?

> Thanks in advance.



Sat, 31 May 2003 06:46:43 GMT  
 Help Me !! some strange in a structure size


Quote:
> Field alignment in structure depends on how the compiler aligns
> the field within the structure (say, 32-bit alignment / 16 (WORD)
> alignment)
> ...
> If you want the actual size you may use the pragma directives to
> *pack* the structures ...
> #pragma pack(1)

#pragma pack(1) is a non-standard feature.  You should at least
point that out.

Many modern CPUs have a real difficulty accessing unaligned data.
Some (notably the RISCs) just don't provide any instruction-level
mechanism to do this, and many others suffer a significant
performance penalty doing it.

Unless memory is very tight, don't do it.  Also, don't count on
being provided a "#pragma pack(1)"-like mechanism on every
platform.

Sent via Deja.com http://www.deja.com/
Before you buy.



Sat, 31 May 2003 08:02:45 GMT  
 Help Me !! some strange in a structure size
It's alignment. Obviously in you case alignment is 4 bytes. It means that
compiler rounds all structures and arrays for multiple of 4 bytes. If you
want to change it you should check which compilation flag changes the
alignment.
          Jane.


Quote:
> Hi..everyone..

> Hmm.. I have a question for a structure size.
> For instance, if I declare a structure following.

> struct s {
>     int * a;
>     int * b;
>     unsigned char c;
> };

> Then, the size of structure s is 9 bytes in 32-bit platform, isn't it ?
> But, as the result of test, it has 12 bytes.

> I had tested it with GNU gcc.

> Why don't have it 9 bytes exactly ?

> Thanks in advance.



Sun, 01 Jun 2003 18:28:32 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Find size of variable size structure?

2. structure size problem (was Re: HELP)

3. Help on structures' sizes

4. Strange, strange, strange

5. strange structure behavior

6. Strange behavior of std:vector used on custom data structure

7. LoadImage Failures for Strange Image Sizes

8. strange sizing behavioir.....

9. Strange behaviour of window size with CRect.

10. Help with structuring a structure:)

11. Need help in declaring a C# structure from C structure

12. structure of array of structure : help needed

 

 
Powered by phpBB® Forum Software