473,396 Members | 1,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 1825
"Bryan Feeney" <bf*****@oceanfree.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**********@enyo.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.com> Kevin Bracey <ke**********@tematic.com> writes:
In message <c7**********@enyo.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 "bookkeeping" 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.learn.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*****@oceanfree.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.com/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$xw3.2495220@attbi_s04> glen herrmannsfeldt <ga*@ugcs.caltech.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
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
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...
19
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...
36
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...
8
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...
47
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...
6
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: ...
10
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
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 <...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
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...
0
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,...

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.