views:

108

answers:

4

Possible Duplicate:
How does a compiled C++ class look like?

Hi all,

 bash$cat struct.c
struct test
{
int i;
float f;
};

bash$gcc -c struct.c

The object file struct.o is of elf format. I am trying to understand what does this object file contain. The source code is just a definition of a struct. There is nothing executable here so there should be nothing in text, and there is no data really either. So where does the definition of struct go really?

I tried using;

readelf -a struct.o
objdump -s struct.o

but don't quite understand this.

Thanks,

Jagrati

+3  A: 

There is nothing. It does not exist. You have created nothing and used nothing.

The definition of the struct is used at compile time. That definition would normally be placed in a non-compiled header file. It is when a struct is used that some code is generated. The definition affects what the compiler produces at that point.

This, among other reasons, is why compiling against one version of a library and then using another version at runtime can crash programs.

Borealid
except maybe into the debugging information if you're generating that - although without a use it may not even go there.
Rup
Hi Borealid, True that usually such a file will be included as a header file and is not likely to be compiled on its own. But say I do compile it and use this object file when compiling another file say main.c which uses definition of this struct (I can do this, right?). Then in that case, there should be something in the object file struct.o which tells about what there was in struct.c to the compiler when compiling main.c.
learnerforever
@learnerforever: No, struct *declaration* is always taken from header file. That's why you have to include header files all the time.
el.pescado
Thanks el.pescado. I was wrong with my hypothesis that I could separately compile a struct definition and link it with say main.c when compiling main.c instead of including the struct header file in main.c.
learnerforever
+2  A: 

structs are not compiled, they are declared. Functions get compiled though.

Alexandre C.
+2  A: 

I'm not an expert and I can't actually answer the question... But I thought of this. Memory is memory: if you use 1 byte as integer or char, it is still one byte. The results depends only on the compiler. So, why can't be the same for structs? I mean, the compiler probably will calculate the memory to allocate (as your computer probably will allocate WORDS of memory, not bytes, if your struct is 1 byte long, probably 3 bytes will be added allowing the allocation of 4 bytes word), and then struct will just be a "reference" for you when accessing data. I think that there is no need to actually HAVE something underneath: it's sufficient for the compiler to know that, in compile time, if you refer to field "name" of your struct, it shall treat is as an array of chars of length X.

As I said, I'm not expert in such internals, but as I see it, there is no need for a struct to be converted in "real code"... It's just an annotation for the compiler, which can be destroyed after the compilation is done.

AkiRoss
It's like that; +1 for the educated guess. :)
Matteo Italia
+2  A: 

So where does the definition of struct go really?

Struct definition usually goes to /dev/null. C does not have any introspection features, so struct definition is not needed at run time. During compilation, calls to struct fields are converted to numeric offsets, eg. x->f would be compiled to equivalent of *((void*)x + sizeof(int)). That's why you need to include headers every time you use struct.

el.pescado
Thanks el.pescado. You solved my problem in one of your comments to this question.
learnerforever
You mean `*(float *)((char *)x + sizeof(int))`. Pointer arithmetic on, and dereferencing of, `void *` are both invalid.
R..