473,765 Members | 2,081 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A simple (?) question about the size of structs

This structure is, according to sizeof, 3 bytes long, which makes sense

struct test
{ char text[3];
};

This structure is, according to sizeof, 4 bytes long, which also makes sense

struct test
{ int number;
};

This structure is, according to sizeof, 8 bytes long, which is confusing the
hell out of me!

struct test
{ char text[3];
int number;
};

Where is the extra byte coming from? It's not something I need to know, but
I'd like to know what's going on. I'm using GCC3 on Windows via MinGW as
supplied by the Bloodshed Dev-C++ IDE (verson 4.9.8.0).
Thanks in advance
--
Bryan
Nov 14 '05 #1
8 1851
"Bryan Feeney" <bf*****@oceanf ree.net> writes:
This structure is, according to sizeof, 3 bytes long, which makes sense struct test
{ char text[3];
}; This structure is, according to sizeof, 4 bytes long, which also makes sense struct test
{ int number;
}; This structure is, according to sizeof, 8 bytes long, which is confusing the
hell out of me! struct test
{ char text[3];
int number;
}; Where is the extra byte coming from? It's not something I need to know, but
I'd like to know what's going on. I'm using GCC3 on Windows via MinGW as
supplied by the Bloodshed Dev-C++ IDE (verson 4.9.8.0).

Your compiler and/or host architecture prefer or require to align
the integer field on a 4-byte boundary. At least under Unix on some
architectures, this may be required to avoid bus-errors occuring at
run-time, or preferred (for speed) so that the integers do not need
aligning at run-time.

That said, there can be other "gotchas" with some compilers/architectures:

struct {
char a;
int i;
char b;
}

The sizeof() this may still be 8 bytes, if the compiler chooses to pack
the two characters into the 4-byte space before (or after) the integer.

It is something of which you should be aware, if writing sizeof(structs)
to files or "down" a network connection.

_______________ _______________ _______________ _______________ _______________ ____
Dr Chris McDonald EMAIL: ch***@csse.uwa. edu.au
School of Computer Science & Software Engineering
The University of Western Australia WWW: http://www.csse.uwa.edu.au/~chris
Crawley, Western Australia, 6009 PH: +61 8 6488 2533, FAX: +61 8 6488 1089
Nov 14 '05 #2
In message <c7**********@e nyo.uwa.edu.au>
Chris McDonald <ch***@csse.uwa .edu.au> wrote:
That said, there can be other "gotchas" with some compilers/architectures:

struct {
char a;
int i;
char b;
}

The sizeof() this may still be 8 bytes, if the compiler chooses to pack
the two characters into the 4-byte space before (or after) the integer.


The rule about structures with common initial sequences in unions restricts
that slightly.

The addition of a member to the end of a structure is not allowed to change
the positions of earlier members.

Thus 'b' could be tucked in between 'a' and 'i', but 'a' couldn't be
placed after 'i'.

Is anyone actually aware of any implementations that do this though?

--
Kevin Bracey, Principal Software Engineer
Tematic Ltd Tel: +44 (0) 1223 503464
182-190 Newmarket Road Fax: +44 (0) 1223 503458
Cambridge, CB5 8HE, United Kingdom WWW: http://www.tematic.com/
Nov 14 '05 #3
In <4b************ ****@tematic.co m> Kevin Bracey <ke**********@t ematic.com> writes:
In message <c7**********@e nyo.uwa.edu.au>
Chris McDonald <ch***@csse.uwa .edu.au> wrote:
That said, there can be other "gotchas" with some compilers/architectures:

struct {
char a;
int i;
char b;
}

The sizeof() this may still be 8 bytes, if the compiler chooses to pack
the two characters into the 4-byte space before (or after) the integer.
The rule about structures with common initial sequences in unions restricts
that slightly.

The addition of a member to the end of a structure is not allowed to change
the positions of earlier members.

Thus 'b' could be tucked in between 'a' and 'i', but 'a' couldn't be

^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^placed after 'i'.


Nonsense!

13 Within a structure object, the non-bit-field members and the
units in which bit-fields reside have addresses that increase in
the order in which they are declared.

There is no way to have 'b' allocated before 'i'. Note that a correct
C program can trivially check this (e.g. using the offsetof macro) so
the as-if rule doesn't apply.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #4
Bryan Feeney wrote:
This structure is, according to sizeof, 3 bytes long, which makes sense

struct test
{ char text[3];
};

This structure is, according to sizeof, 4 bytes long, which also makes sense

struct test
{ int number;
};

This structure is, according to sizeof, 8 bytes long, which is confusing the
hell out of me!

struct test
{ char text[3];
int number;
};

Where is the extra byte coming from? It's not something I need to know, but
I'd like to know what's going on. I'm using GCC3 on Windows via MinGW as
supplied by the Bloodshed Dev-C++ IDE (verson 4.9.8.0).
Thanks in advance
--
Bryan


Here is the rule on the sizes of structures:
The size of a structure may not be the sum of the sizes of its fields.

Reasoning:
Compilers are allowed to insert "padding" bytes between fields.
Compilers can also add "bookkeepin g" information in the structure
as well.

Some compilers like to keep the fields "aligned" to certain address
boundaries, such as 16-bit and 32-bits. This helps processors
access the data in a more efficient manner.

Mapping Structures To Data Or Hardware
Because of the padding rule, structures cannot be portably used to
map data to external formats (such as message formats or hardware
registers). One may keep the structure, but there must be code
to convert the external layout to the internal one.

Given a message header of:
command [8-bits]
length [16-bits]
The structure of:
struct Message
{
unsigned char command; /* CHAR_BITS == 8 */
unsigned short length;
};
allows the compiler to insert padding between the command and
length fields, so that they won't match the incoming data. And
not forgetting that an "unsigned short" or even "unsigned int"
may be larger than the 16-bits in the message.

However, once the data is placed into the structure variables,
the program can reference the fields with no problem. It's
just that the data has to be reformatted when outputting.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.l earn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Nov 14 '05 #5
Bryan Feeney wrote:
This structure is, according to sizeof, 3 bytes long, which makes sense

struct test
{ char text[3];
};

This structure is, according to sizeof, 4 bytes long, which also makes sense

struct test
{ int number;
};

This structure is, according to sizeof, 8 bytes long, which is confusing the
hell out of me!

struct test
{ char text[3];
int number;
};

Where is the extra byte coming from? It's not something I need to know, but
I'd like to know what's going on. I'm using GCC3 on Windows via MinGW as
supplied by the Bloodshed Dev-C++ IDE (verson 4.9.8.0).
Thanks in advance
--
Bryan


I'm really surprised that nobody has pointed out that this is a FAQ.

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

-Peter

Nov 14 '05 #6
On Wed, 12 May 2004 14:21:30 +0100, in comp.lang.c , "Bryan Feeney"
<bf*****@oceanf ree.net> wrote:
This structure is, according to sizeof, 8 bytes long, which is confusing the
hell out of me!

struct test
{ char text[3];
int number;
};

Where is the extra byte coming from?


FAQ 2.12 and 2.13

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.c om/ms3/bchambless0/welcome_to_clc. html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 14 '05 #7
Dan Pop wrote:

(someone wrote)
struct {
char a;
int i;
char b;

(someone else wrote)
Thus 'b' could be tucked in between 'a' and 'i', but 'a' couldn't be
Nonsense! 13 Within a structure object, the non-bit-field members and the
units in which bit-fields reside have addresses that increase in
the order in which they are declared. There is no way to have 'b' allocated before 'i'. Note that a correct
C program can trivially check this (e.g. using the offsetof macro) so
the as-if rule doesn't apply.


Can the compiler detect use of the offsetof macro?

There are stories related to SPEC and tricks compilers
use when they discover that they are compiling a SPEC program.

Or maybe only when a certain compiler flag is set to allow it.

-- glen

Nov 14 '05 #8
In <9RDoc.37479$xw 3.2495220@attbi _s04> glen herrmannsfeldt <ga*@ugcs.calte ch.edu> writes:
Dan Pop wrote:

(someone wrote)
struct {
char a;
int i;
char b;
(someone else wrote)
Thus 'b' could be tucked in between 'a' and 'i', but 'a' couldn't be
Nonsense!

13 Within a structure object, the non-bit-field members and the
units in which bit-fields reside have addresses that increase in
the order in which they are declared.

There is no way to have 'b' allocated before 'i'. Note that a correct
C program can trivially check this (e.g. using the offsetof macro) so
the as-if rule doesn't apply.


Can the compiler detect use of the offsetof macro?


What the compiler cannot detect is the usage of the offsetof macro
in another translation unit using the same structure ;-)
There are stories related to SPEC and tricks compilers
use when they discover that they are compiling a SPEC program.


What is not a story is that many commercial compilers have their
optimisers tuned for the SPEC programs. As a result, the compiler
generates optimal code for the SPEC programs naturally, without making
any attempt to recognise them.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #9

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

Similar topics

7
2965
by: Wellu Mäkinen | last post by:
Hi all, let's say I have a need to store 4 int values in somekind of a structure. I have atleast two options: class A { public: int a, b, c, b;
5
3288
by: John | last post by:
Hi all, Can a linked list be a member of a structure? If so, when I add or remove an element from the linked list, the size of the structure will change. Will it cause any problem? Thanks a lot. John
19
1760
by: Method Man | last post by:
I understand that arrays and structs can't be passed by value into and out of functions since they can be arbitrarily big. My question is: Why are types allowed to be passed by value? Couldn't my types be arbitraily big as well?
36
7789
by: Bhalchandra Thatte | last post by:
I am allocating a block of memory using malloc. I want to use it to store a "header" structure followed by structs in my application. How to calculate the alignment without making any assumption about the most restrictive type on my machine? Thanks.
8
5113
by: Ross A. Finlayson | last post by:
I'm trying to write some C code, but I want to use C++'s std::vector. Indeed, if the code is compiled as C++, I want the container to actually be std::vector, in this case of a collection of value types or std::vector<int>. So where I would use an int* and reallocate it from time to time in C, and randomly access it via , then I figure to copy the capacity and reserve methods, because I just need a growable array. I get to considering...
47
2656
by: sunglo | last post by:
Some time a go, in a discussion here in comp.lang.c, I learnt that it's better not to use a (sometype **) where a (void **) is expected (using a cast). Part of the discussion boiled down to the rule: if I cast a (sometype **) to a (void **) I am making a number of assumptions about the implementation's (void **) representation and length. Specifically, if I do the above cast I'm assuming that a (sometype **) and a (void **) have the same...
6
5010
by: Robbie Hatley | last post by:
I'm maintaining a software project with 134 C++ files, some of them huge (as much as 10,000 lines each), and very few prototypes. The author's attitude towards prototypes was like this: Prototypes are only good for headers to be included in other files. For functions which call each other inside one file, such as A calls B which calls C and D, just define the functions in order D, C, B, A, and you'll never need prototypes.
10
1920
by: Potatoman | last post by:
i have some an array of structs, i need to compare arrays. what can you do me for ?thanx Potatoman
37
2194
by: Hilton | last post by:
Hi, for (int i = 0; i < list.Count; i++) has a hidden performance hit; i.e. list.Count gets evaluated each time, so we write something like: int listCount = list.Count; for (int i = 0; i < listCount; i++)
0
9568
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10156
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10007
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9832
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8831
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7375
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5275
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3531
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.