Who decides the Heap of an application ?

Heap is dependent upon the RAM's free memory size which is determined by the OS. Correct ? But how ?

I believe that the OS while getting compiled will have a heap definition based on the linker script file / Linker Control File (LCF).

If i am making an application which has some mallocs and callocs in it. And now the Application's Heap is inturn dependant on the OS's heap & the Linker Control File of the application . Correct ?

But, if there is no Linker Control File for the application(a simple application) , then the compiler will inturn interpret the OS Heap memory and allocate heap for this application (during the linking process). Correct ?

Is my above assumption correct ? R there other dependencies ?

CPU architecture decides the direction of Heap Growth. Correct ? Does CPU have any role w.r.t Heap ?

In a small function as below : void func(void) { int i = 1; if(i==5) { printf(" Hello \n "); } } But, Where will the value '5' be present during this 'IF comparison statement' ?

Clarifications needed, Thx in advans, Karthik Balaguru

Reply to
Loading thread data ...

The heap is primarily managed by the C library functions (malloc, calloc, free, and possibly some implicit init calls during startup). Depending on the system, the application either gets a fixed chunk of memory at startup, or there is an OS call to extend the process memory size by a certain amount. The malloc() library code is then responsible for dividing up the big chunk of memory, and handing it to the C program. If the memory runs out, and if the OS supports it, the malloc() code may request another chunk from the OS.

On a system without an OS, the initial chunk size may be determined by linker script file, or by some other system specific way.

The CPU plays no role beyond executing the library code.

If the compiler is smart, it will see the if() is never taken, so it won't even include any of the code.

A less optimizing compiler may use an immediate compare instruction. The constant '5' will typically be encoded in the instruction itself.

Reply to

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.