tags:

views:

186

answers:

7

I am wondering why the following two types

struct {
    double re[2];
};

and

double re[2];

have the same size in C? Doesn't struct add a bit of size overhead?

Thank you in advance.

+15  A: 

No, it just merely composes all the elements into one higher-level element whose size is merely the individual elements' sizes added up (plus some padding depending on alignment rules, but that's out of the scope of this question).

Jim Buck
+1 for mentioning alignment.
Ayman Hourieh
For the effect of alignment in structs see http://www.unidata.ucar.edu/blogs/default/2009/03/30/1238442300000.html
VolkerK
+7  A: 

Not if it can help it - no. C avoids overhead like the plague. And specifically, it avoids overhead in this context.

If you used a different structure, you might see a difference:

struct space_filled
{
    char       part0;
    double     part1;
};

If your machine requires double to be aligned on an 8-byte boundary (and sizeof(double) == 8, which is normal but not mandated by the standard), then you will find that the structure occupies 16 bytes.

Jonathan Leffler
abelenky
Not necessarily - if there was another char, or a short or an int, or one of each in sequence (under fairly normal size assumptions), then there would be no space. But in the absence of such 'filler' variables, you are right. (And I don't think the C standard guarantees that the local variables will be laid out in any particular order relative to each other.)
Jonathan Leffler
@Jonathan: I'm pretty sure it doesn't, so the compiler could order automatics however it likes to save space, if it wanted to. In fact, as long as a pointer to the automatic doesn't leave the local scope, I don't think automatics need to be on stack at all - compiler can keep them in registers if it likes. I think.
Steve Jessop
+3  A: 

No. Struct does not add any size, or have any overhead in the compiled C.

It is a layer of syntax that requires additional work by the compiler, but has no overhead at runtime.

C is an extremely "naked" language, meaning that nothing is there unless required. So ask yourself, "What overhead does a struct REQUIRE?", and you won't find any.

abelenky
+1  A: 

No it doesnt.

That's one of the good points of structs (why they were so helpful in old school TCP/IP programming).

It's a good way to represent the memory/buffer layout.

Alan
A: 

no the struct type in C just sequentially layout the members in memory

bashmohandes
+1  A: 

Nope, the struct doesn't have to add anything. Unlike in Java or .NET, where classes (and structs) have a bunch of other responsibilities, in C and C++, they are simply containers used to hold a number of data members. In C++, they may have to store a vtable to resolve virtual function calls if any exist, but in general, no, a struct itself has no overhead.

The one exception is this:

typedef struct {} empty;
assert(sizeof(empty) > 0);

The size of an empty struct will not be zero. A struct has to have some nonzero size since every object has to have a unique address. (Otherwise you wouldn't be able to create an array of these structs)

jalf
+1 for mentioning the empty struct.
Evan Teran
A: 

sometmes, see: http://en.wikipedia.org/wiki/Sizeof

Ray Tayek