Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > sizeof a struct

Reply
Thread Tools

sizeof a struct

 
 
Phui Hock
Guest
Posts: n/a
 
      11-06-2003
Hi,
I'm a newbie in C. Can anyone tell me why the size of 'test' is 8
bytes instead of 4 bytes when I uncomment the 'struct list *next;'? I
hope someone can can explain to me. I can't find answer anywhere in my
references nor on the net. Thank you very much.

#include <stdint.h>

typedef struct list{
uint8_t a :6;
uint8_t b :1;
uint8_t c :1;
struct {
uint8_t x :4;
uint8_t y :1;
uint8_t z :3;
} inner;
/* struct list *next; */
} test;

int main(){
printf("size of test is %d\n", sizeof(test));
return 0;
}
 
Reply With Quote
 
 
 
 
Ben Pfaff
Guest
Posts: n/a
 
      11-06-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (Phui Hock) writes:

> I'm a newbie in C. Can anyone tell me why the size of 'test' is 8
> bytes instead of 4 bytes when I uncomment the 'struct list *next;'? I
> hope someone can can explain to me. I can't find answer anywhere in my
> references nor on the net. Thank you very much.
>
> #include <stdint.h>
>
> typedef struct list{
> uint8_t a :6;
> uint8_t b :1;
> uint8_t c :1;
> struct {
> uint8_t x :4;
> uint8_t y :1;
> uint8_t z :3;
> } inner;
> /* struct list *next; */
> } test;


The compiler is padding the 16 requested bits of bit-fields to 4
bytes, probably for performance reasons. Compilers are allowed
to add padding after structure members.
--
"IMO, Perl is an excellent language to break your teeth on"
--Micah Cowan
 
Reply With Quote
 
 
 
 
Andreas Kahari
Guest
Posts: n/a
 
      11-06-2003
In article <(E-Mail Removed) >, Phui Hock wrote:
> Hi,
> I'm a newbie in C. Can anyone tell me why the size of 'test' is 8
> bytes instead of 4 bytes when I uncomment the 'struct list *next;'? I
> hope someone can can explain to me. I can't find answer anywhere in my
> references nor on the net. Thank you very much.


Read this FAQ:

http://www.eskimo.com/~scs/C-faq/q2.13.html

--
Andreas Kähäri
 
Reply With Quote
 
Alex
Guest
Posts: n/a
 
      11-06-2003
Phui Hock <(E-Mail Removed)> wrote:
> Hi,
> I'm a newbie in C. Can anyone tell me why the size of 'test' is 8
> bytes instead of 4 bytes when I uncomment the 'struct list *next;'? I
> hope someone can can explain to me. I can't find answer anywhere in my
> references nor on the net. Thank you very much.


> #include <stdint.h>


> typedef struct list{
> uint8_t a :6;
> uint8_t b :1;
> uint8_t c :1;
> struct {
> uint8_t x :4;
> uint8_t y :1;
> uint8_t z :3;
> } inner;
> /* struct list *next; */
> } test;


First of all, you are relying on the pointer to be of certain size.
The C standard does not define how big such a pointer should be,
it is up to your implementation.

Second of all, your compiler is free to insert "padding" where ever
it sees fit within the structure[1] in order to facilitate alignment
that is more natural for your architecture or for other murkier
reasons.

[1]: There can never be padding before the first element.

Alex
 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      11-06-2003
Phui Hock wrote:
> Hi,
> I'm a newbie in C. Can anyone tell me why the size of 'test' is 8
> bytes instead of 4 bytes when I uncomment the 'struct list *next;'? I
> hope someone can can explain to me.


Sure. A pointer is a variable, so it has a size in bytes. What did you
expect ?

I would say that pointers are probably 4 bytes long on your platform,
but they also may be shorter and your compiler has added extra padding bits.

Take care, the size of struct may not equal the total of it's members
sizes.

> I can't find answer anywhere in my
> references nor on the net. Thank you very much.


> #include <stdint.h>


> typedef struct list{
> uint8_t a :6;
> uint8_t b :1;
> uint8_t c :1;
> struct {
> uint8_t x :4;
> uint8_t y :1;
> uint8_t z :3;
> } inner;
> /* struct list *next; */
> } test;
>
> int main(){


it's int main(void) or int main(int argc, char **argv)

> printf("size of test is %d\n", sizeof(test));


call of printf() without definition
(hint : you need to #include <stdio.h>)

> return 0;
> }



[laotseu@localhost dev]$ gcc -Wall -ansi -pedantic -obytes bytes.c
bytes.c:9: warning: bit-field `x' type invalid in ISO C
bytes.c:10: warning: bit-field `y' type invalid in ISO C
bytes.c:11: warning: bit-field `z' type invalid in ISO C
bytes.c:5: warning: bit-field `a' type invalid in ISO C
bytes.c:6: warning: bit-field `b' type invalid in ISO C
bytes.c:7: warning: bit-field `c' type invalid in ISO C

Woops. Non portable code ?

With line 13 commented out :
[laotseu@localhost dev]$ ./bytes
size of test is 2

With line 13 uncommented :
[laotseu@localhost dev]$ ./bytes
size of test is 8

Did I say something about padding ?-)
(NB : pointers are 4 bytes long on my platform.)

Bruno

 
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
#define ARR_SIZE sizeof(arr)/sizeof(arr[0]) Vinu C Programming 13 05-12-2005 06:00 PM
sizeof(enum) == sizeof(int) ??? Derek C++ 7 10-14-2004 05:11 PM
struct my_struct *p = (struct my_struct *)malloc(sizeof(struct my_struct)); Chris Fogelklou C Programming 36 04-20-2004 08:27 AM
sizeof(str) or sizeof(str) - 1 ? Trevor C Programming 9 04-10-2004 05:07 PM



Advertisments