Microsoft Interview Question
Software Engineer / DevelopersCountry: India
Interview Type: In-Person
Initially both segments are allocated with some fixed amount of memory, when either of it gets overflown, The information is signaled to the OS, where it extends the overflow limit. when it is extending, it might check for the overlapping condition.
There will be break pointer which marks the end of the heap area. When it requires more memory space it will be extended by using system call such as brk()
There is no hard-n-fast rule of stack grows downwards and heap upwards...it depends on operating systems , and the way they handles the memory if the operating system is present in lower memory area then stack generally grows downwards and heap upwards. And For collision how a complier can decide that stack is going to collide with heap so this thing happens dynamically. Hope you remember the concept of segments on x86 architecture , there are segments like stack segment , so segments will no allow you a stack to go inside the heap area.
Actually it is not a RULE that a stack will always grow downwards and the heap will always grow upwards. It depends on the OS that you are using. But ya in most of the modern systems stack generally grows downwards and heap grows upwards.
- Spock July 09, 2012In the modern system there is actually no chance of collision. If the stack is full and it tries to enter into the heap area then you will get a stack overflow error. On the other hand if heap is full then the call to "malloc()" will return null. Now it depends on the programmer to check whether malloc() returned null or not.
Hope I am clear.. :)