473,465 Members | 4,818 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

sizeof a struct

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;
}
Nov 13 '05 #1
4 11092
ph******@myway.com (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
Nov 13 '05 #2
In article <a9**************************@posting.google.com >, 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
Nov 13 '05 #3
Phui Hock <ph******@myway.com> 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
Nov 13 '05 #4
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

Nov 13 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Ronald A. Andersen | last post by:
********** header **************** struct articleFile2 { char CR; int intStampDate; }; *********** source *************** struct articleFile2 *ptrArticle2 = NULL;
19
by: Martin Pohlack | last post by:
Hi, I have a funtion which shall compute the amount for a later malloc. In this function I need the sizes of some struct members without having an instance or pointer of the struct. As...
33
by: Chris Fogelklou | last post by:
What is wrong with the above? Don't worry, I already know (learned my lesson last week.) It is for the benefit of our resident compiler guru who seems to think you need the cast. I thought it...
10
by: Sean | last post by:
I have a struct that I wrote to test a protocol. The idea I had was to just declare the elements of the struct in the order in which they are sent and received as defined by the protocol. ...
10
by: Mark A. Odell | last post by:
Is there a way to obtain the size of a struct element based only upon its offset within the struct? I seem unable to figure out a way to do this (short of comparing every element's offset with...
12
by: news.fe.internet.bosch.com | last post by:
Hi All , I u find out size of struct , does it considers paddding chars into consideration struct A { char c; int i; };
18
by: Mockey Chen | last post by:
My friend ask me a question as following: give a union SU define as: typedef union _SU { short x; struct y{ char a; short b; char c;
8
by: Chameleon | last post by:
I have a TGA image header struct. TGA has 18 bytes header, so the C struct too. why this return 20? sizeof(TGAHeader) I saw this in many structs. I believe compiler round up the size to 4...
72
by: goacross | last post by:
char ch='a'; int v=sizeof ++ch; cout<<ch<<endl;// output: 'a' why not 'b'? thanks
14
by: ManicQin | last post by:
Hi all. I'm trying to get the size of a variable in a struct by his relative postion i.e. /// #define offsetof(s,m) (size_t)&(((s *)0)->m) struct ThePimp{ char rings; char blings;
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.