Tuesday, March 11, 2008

Size of an empty struct

Size of Empty struct

When I was teaching, some students used to ask me a question. Why is size of an empty struct 1 in C++ and 0 in C ? I coded and found out that gnu compiler does give this result whereas other compilers like tc and vc++ give an error for empty structure in c ? What is the reason?
#include
struct emptystr
{
};
int main()
{
printf(“%d”,sizeof(struct emptystr));
}

Compile it under gcc. The answer is 0 and under g++ answer is 1.
It can be explained like this . Let us say an empty struct has a size of zero. Then two adjacent variables of this structure type must have the same address. Address of any variable must be unique. Hence it is not possible to have size of any variable or data type as zero.
Then how do you explain the fact that in C, size is 0. The actual reason is, according to ANSI standard, sizeof operator can not be applied to incomplete data types. An empty structure is incomplete. Hence 0 we are getting is garbage. Try to compile this program with flag –pedantic-errors. You will get a syntax error.

gcc emptystr.c –pedantic-erros

You will get an error which says struct has no members.

Then how is empty structure valid in C++ ? C++ being OOP language, a structure can contain methods as well as data members. Even though the structure has no data members, there are at least a constructor and destructor which are supplied by compiler.
Hence the type is not incomplete. (well it is in fact similar to a structure or class with only function members )

struct onlyfuntcions{
void print()
{
Cout<<”Hello”;
}
};
Size of this structure in c++ is also 1 byte.
Now don’t get scared. You did not know that a struct can also have constructor?? It can have. Struct in C++ is very much similar to class. The only difference between the two is struct members by default are public where as class members by default are private.

Books for c

c is not only c in its concepts but also its associated problems. First and foremost problem is which book should you refer. Half the population (students - non pros) vouch for Let Us C by Yeshwant Kanetkar. Other half hate Let Us C and advice you to buy, read and refer C bible that is The C programing Language by Kernighan and Ritchie.
The former is meant for kids ( or kids in c) and latter is for pro who already knows everything in C. Let us C is easy to understand but it should not be used after you learn the alphabets of C. K&R is good but cryptic. Now you have learnt the alphabets, but not mastered the grammars and prose and verse of the language. So how do you bridge the gap ? Or which book bridges the gap. Now there are Balaguruswamys, Stephen Cochens etc. Along with these there is c-faq, a set of very good questions and good answers to them by Steve summit. It helps a lot.
Recently I have come across a book called C in a Nutshell by Peter Prinnz. This is again a reference. You should not expect lengthy explanations or solved programs. But all the concepts are given and without any ambiguity. THe book also covers ANSI-99.