sizeof a struct

P

Phui Hock

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;
}
 
B

Ben Pfaff

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.
 
A

Alex

Phui Hock said:
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
 
B

Bruno Desthuilliers

Phui said:
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 said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top