1

As conceptually stated, the stack grows down and the heap up; is there a point for both to collide? If yes, then does the "stack overflow" happen when the stack grows and crosses its limit? Can't it enter into the heap section?

The heap also grows upwards so its size is also fixed, I think. But nowhere is it mentioned how much memory is allocated for the heap. When I tried on my computer, both stack and heap memory addresses are going upward.

It's not clear conceptually for me.

trincot
  • 317,000
  • 35
  • 244
  • 286
  • What method did you use to determine that heap and stack addresses are going upward? You should include this code and the results that you got. – Alain Merigot Jan 27 '19 at 10:20
  • I guess you're thinking about an image such as [this one](http://www.andrew.cmu.edu/course/15-440-s12/applications/ln/proccontext.jpg). This is pretty old nowdays, and modern paged and virtual memory operating systems doesn't work like that. – Some programmer dude Jan 27 '19 at 10:23
  • @AlainMerigot, actually i created variable in both the place and print the address and got to know – Tuhin Panda Jan 27 '19 at 10:26
  • @Someprogrammerdude,not only that pic, but there are lot place where same thing is mentioned. can you please clear my doubt about the stack and heap overlapping – Tuhin Panda Jan 27 '19 at 10:28
  • In the "old" days of DOS and whatnot, some operating systems allocated a fixed size chunk for each running program, and that chunk could not be extended. That's what you see in such an image. Today, the operating systems uses *pages* allocated from *virtual memory*, and then the pages can be mapped at any valid address. I suggest you do some more research about *virtual memory* and *paging*. – Some programmer dude Jan 27 '19 at 10:37
  • @Someprogrammerdude, thanks , i will do , but just last question, stack over comes when stack memory got crossed or no memory available ? – Tuhin Panda Jan 27 '19 at 10:42
  • The stack is a fixed-size and limited resource. Windows, for example, allocates a single MiB of memory per process for the stack. If you use more then you have a stack overflow. The same that happens in the old image when the heap and stack meets. – Some programmer dude Jan 27 '19 at 10:46
  • Of interest: https://stackoverflow.com/a/53699971/56778 – Jim Mischel Jan 28 '19 at 14:11

1 Answers1

0

The stack grows accordingly to the platform. Look at this question for details.

Besides the direction when the stack grows, when you need to increase the stack size, the Memory Managers asks for a contiguos virtual memory block in the direction it has to grow, if that request can be satisfied, your stack grows, otherwise the request fails.

The failure is handled differently, according to hardware architecture, operating system, and configurations. For example, a standard user mode application on windows generally fails with a "not enough memory to execute the operation", but the failure could vary as corruption could occur.

Each thread is allocated a range of contiguous virtual addresses upon start. This value is decided in order by the compiler, which can be overridden by you and finally overriden by the operating system. You can smash a process by simply starting a sufficient number of threads without doing number (aside from handle exhaustion which is irrelevant here).

Yennefer
  • 5,704
  • 7
  • 31
  • 44