After going through lots of stuff about declaration and definition,in a nutshell I understand that the difference is in the memory allocation.
In definition yes and in declaration no memory is allocated.
Fine ,Is there any process that shows that yes

int a; //which is a definition

allocates a 4 bytes memory by the name a.
gcc generates an assembly code ,but seing that assembly I don't find any presence of variable a in the assembly code generated by gcc.
Like in assembly it is supposed to be something like

a     dword        ?

Well above code is masm code. So something similar to above.
But there is no trace of int a in the generated assembly,then how can we say it as a definition.
Or is there any way to find out the offset given to int a,without using a single second statement .(basically via assembly code).
Please rectify me if I am going wrong.

Cheers!!

Recommended Answers

All 5 Replies

There probably will not be a definition of a like that. If the variable is local, it will probably be defined as an offset on the stack. Look for changes to esp and ebp:

void Function()
{
    int a;

Might look like this for whatever flavor of assembly is output. I do not know gas well enough to give you an example of it ;):

; create a stack frame with a local int variable
push ebp
mov ebp, esp
sub esp, 4

> int a; //which is a definition
Actually, this is just a tentative definition
At the point of compiling it, storage space is not yet allocated to it, so you won't find it as such in the object file.

If you said nothing else, then the linker would create a single
int a = 0;
for you.

Or if you had exactly ONE
int a = 42;
in one of your source files, then all the other int a; would refer to that.

commented: Very interesting link :) +19

> int a; //which is a definition
Actually, this is just a tentative definition
At the point of compiling it, storage space is not yet allocated to it, so you won't find it as such in the object file.

Isn't so a virtual address is allocated to it by the compiler.

Or if you had exactly ONE
int a = 42;
in one of your source files, then all the other int a; would refer to that.

I think thats when int a is a global variable.
Cheers!!

>Isn't so a virtual address is allocated to it by the compiler.
Hmm... I'm not 100% about the internals of gcc, but dosen't the compiler handle the variable's for any given object (including "variable's" not yet given by the linker), and the linker set up the stack when interacting with others?

> Isn't so a virtual address is allocated to it by the compiler.
It's a directive generated by the compiler to the linker, which basically says

If there is an actual definition of 'a', then use that.
If there is no definition of 'a' anywhere, then make an a=0 and use it.

$ cat prog.c && gcc prog.c && ./a.out 
#include <stdio.h>

int a;

int main ( ) {
  printf( "%d\n", a );
  return 0;
}

0
$ cat prog.c && gcc prog.c && ./a.out 
#include <stdio.h>

int a = 42;

int main ( ) {
  printf( "%d\n", a );
  return 0;
}

42
$ cat prog.c && gcc prog.c && ./a.out 
#include <stdio.h>

extern int a;

int main ( ) {
  printf( "%d\n", a );
  return 0;
}

/tmp/cca1uyC4.o: In function `main':
prog.c:(.text+0x12): undefined reference to `a'
collect2: ld returned 1 exit status

First one is a tentative definition (assumed 0).
Second is a proper definition with an assigned value.
Last one is a declaration (which doesn't exist elsewhere), so it becomes unresolved at link time.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.