-2

why does the compiler not know the addresses of dynamic variables in C (e.g. of dynamic arrays)?

Is it because the size of the variable(array) is not declared before the program is compiled?

Or, I guess, a better question is: how does the complier assign the addresses of static variables(or arrays) when it compiles a program?

zx485
  • 28,498
  • 28
  • 50
  • 59
  • possible duplicate of [Where in memory are my variables stored in c?](http://stackoverflow.com/questions/14588767/where-in-memory-are-my-variables-stored-in-c) – Keith Nicholas Feb 04 '15 at 01:58
  • static and global variables are generated by the compiler at compile time in the data segment; uninitialised ones are put into a part of data segment called "bss segment", which is filled with zeroes. variables allocated at run time are stack and heap variables. Where they are depends on the memory layout at the time they are created. – Einheri Feb 04 '15 at 02:03
  • 1
    What is a "dynamic array"? – John Zwinck Feb 04 '15 at 02:05
  • well from what i have been told in the lecture, it is when an address is declared ex int *b and then you call *b = malloc(4*sizeof(int)) in the main function, then this would create an array with size 4. – Peter Chung Feb 04 '15 at 02:14
  • @PeterChung I think your lecture isn't doing you any favour by calling that a "dynamic array". The name is "heap variable". What's happening is that when the process starts, it's given an area in its address space called "heap". The C library has functions (malloc, free and friends) that lets you reserve memory from that region for your use. It grows from a low address upwards, so when you do call "malloc", what memory it hands back to you depends on what the heap looks like at run time. There's no way for the compiler to know it. – Einheri Feb 04 '15 at 03:56

1 Answers1

0

The compiler will start to compile each source file independently. When it comes to a static variable it stores it in the .bss or .data section depending on if its initialised or not. all the segments of all the objectfiles are put together to something which is called a program memory map if your main function is calling a function of another source file the address of the called function will be a 'dummy' adress which gets replaced during the linking process. Static variables arent exported to the linker that's the reason why no other source files can acces the static variable of another source file because the linker can't see it. So imagine the compiler wouldn't throw an error during the compilation process it would throw an error when all the object files get linked together.

If you want to try it out step by step compile two source files to object files with

gcc -c source1.c -o object1.o
gcc -c source2.c -o object2.o

And then link it together with

ld object1.o object2.o 

If you want to see that static is just a keyword for the linker you can do it like that

Give source1.c a header file of source2.c with a variable or array called test but without the keyword static. in source2.c you define the same variable with the keyword static please don't include the header of source2.c in source2.c itself. And then do the three steps again

You should get a linker error when you call ld