When I write
int main()
{
int j;
}
The memory for 'j' is allotted at the time of compilation,but when during compilation ? What are the various stages of compilation when memory is allotted to a variable? What if j was global ?
When I write
int main()
{
int j;
}
The memory for 'j' is allotted at the time of compilation,but when during compilation ? What are the various stages of compilation when memory is allotted to a variable? What if j was global ?
Not at the time of compilation, your 'int j' will get allocated at application startup, when the application enter main() scope (actually it will not technically get allocated, as the stack is being used), globals will get allocated at runtime before entering main() scope.
It is up to the compiler where to place j. Usually local variables are placed on the stack and as such for your particular example, the compiler will probably reserve the space on the stack for the duration of the main function. Note that this is different from global variable memory, which may receive its own memory.
I guess you are mixing things up.
Compiler doesn't allocate memory for variables - it generates code that allocates memory for variables at runtime. For globals is will be added to program start-up code.
The compilation process doesn't allocate the memory. It generates the code that allocates the memory :)
In this case j would be a so-called stack variable and it would be allocated when execution enters the main() function. Global and static variables are allocated on the heap instead.
Here's a short explanation: http://www.costech.or.tz/cs231/websites/C%20Programming/www-ee.eng.hawaii.edu/Courses/ee150/Book/chap14/subsection2.1.1.8.html. I'll see if I can find a better one.
Compilation generates the executable code for a program. Program memory is allocated when that executable code is run.
The memory isn't allocated at the time of compilation, but at runtime. The compiler just generated machine code that will execute your program, actual allocations happen at runtime. In this case the variable isn't used and there won't emitted any code for it.
I think you are looking at the stages of compilation, rather than the memory allocation of 'j'. Since I think so, here is what happens:
Once you feed your source code to the C compiler, the first stage(s) is(are) lexical and semantic analysis in which syntax and the semantics of the source code are analysed for correctness. If an error(s) was found, the compiler reports accordingly and does not go ahead. If no errors were found, it proceeds to the generation of a intermediate representation of the source code, usually after various optimisations. This intermediate representation can be in a native language (native to the OS/architecture, like in C) or a platform independent bytecode (like Python/Java..). The function of the compiler ends here.
Memory allocation happens only when you execute the code. This is the runtime of the program. This comes only after the compilation stage, which probably you wouldn't want to know here. If you want to, please let me know. I shall try and add whatever I know.
HTH.
In C, main is compiled the same as every other function: any variables declared in main will be "allocated" on the stack. A stack frame is the portion of the stack that is used by a single function call. The frame contains slots for all of the locals used within a function. This memory is considered temporary since when the function returns, this frame will be popped off the stack.
The C compiler will assign a static address to global variables. This address is considered part of the binary's "image" and as such has a static location in memory. The C compiler knows the size of every type, so it can set aside the appropriate amount of space in the memory layout of the binary for each global variable. Then, any code that accesses this variable will simply reference this address instead.
You can examine a variable's address with code like this:
#include<stdio.h>
int i;
void foo(int n)
{
if(n > 2)
return;
printf("From foo &n = %xd\n", &n);
printf("From foo &i = %xd\n", &i);
foo(n+1);
}
int main()
{
printf("&i = %xd\n", &i);
foo(0);
return 0;
}
Running this code produces output similar to:
./a.out
&i = 600934d
From foo &n = 38bc4efcd
From foo &i = 600934d
From foo &n = 38bc4eccd
From foo &i = 600934d
From foo &n = 38bc4e9cd
From foo &i = 600934d
There are two things you should notice here:
foo
changes with each call to foo. In fact, it will decrease every time, since the stack grows downward.Ok friends,
Just tell me that if i wrote
int a=5;
The memory is allocated for 'a' is when ??
COMPILE TIME OR RUN TIME ??
If your answer is COMPILE TIME ... then tell me that if i compiled 10 programs and leave it as it is without run, then it consumes all the memory.And if complier allocates memory than how it deallocate memory.
If your answer is RUN TIME ... then whats the difference between malloc and simple allocation.
First, let me define three terms:- 1. association. 2. reserve. 3. allocated.
We guess that at compile time, it was decided that 'a' has take 2 bytes. And in symbol table that is updated that 'a' reqiured 2 byte. ASSOCIATION.
In linking time, 'a' got a memory location , that 'a' is going to take memory from say 100 and 101 position. So RESERVE.
And, at the run time it actually get the memory block. ALLOCATED
Whereas in declaration like malloc(), all the three happened at RUN TIME.
CORRECT IF I AM WRONG !!!!!