views:

45

answers:

2

The way automatic variables/local variables go on to stack,dynamically allocated objects/data type go on to heap; where is the memory for library function calls (say printf()) allocated. In which segment?

+2  A: 

Library functions aren't really treated differently from whatever other modules you're linking in: their local variables use the stack, their dynamically allocated memory parts go on the heap.

snemarch
+5  A: 

Static linking

For a statically linked program, library code is merged in with the application and almost all of the distinction between program and library is lost, i.e., each object ends up in the same section that a similar object in the main program would occupy.

Dynamic linking

For dynamically linked programs, if an object is writable and not automatic then memory pages will be allocated in each process that uses the library and a data section (or sections) will exist just for the dynamically loaded libraries.

Auto

Automatic variables are allocated on the stack in the same way for the main program, statically linked library functions, and dynamic libs. The linking process doesn't have any role in this, rather, the generated code simply subtracts a specific amount from the stack pointer for each routine's local automatic space requirements.

Local non-auto

Local static variables are allocated by the linker in the way as module-static and global addresses, they just don't have globally linkable names.

Heap

Finally, library routines will link with the same malloc() (or whatever) and so all heap allocations will be made in the same way from the same group of addresses.

DigitalRoss
perfect! Thanks a lot @DigitalRoss
Kedar