![]() The stack can be viewed similarly to a stack of books. To properly understand the call stack you need to understand the stack data structure. Once the execution of that method has finished it is removed (popped) from the call stack and execution is returned to the previous method. To hold the values (or pointers) of local variables used in the methods.Įach time you call a method in your application it is added (pushed) to the call stack along with any local variables that are declared in the call stack. To keep track of the method that control should return to once execution has finished for the current method. The stack has 2 main responsibilities when your program is running: This is why if you run a recursive function in an infinite loop you will get a stack overflow exception.Įven though the stack and heap take up space towards each other the operating system will make sure that they don’t consume the same address space. However, the size of the stack is generally fixed when the application is compiled. Items are added to the stack moving downwards going from high address space to low address space. The stack is located in the high address space. As shown in the diagram the heap grows upwards and more space is allocated to it as you need it, either manually by the programmer (C/C++) or automatically by the runtime. The stack and heap both share the same address space. This is slightly simplified as there are other areas of memory but this is enough to cover what we are looking at today.Īt runtime, your compiled code is stored in memory as execution instructions in the bottom part of memory (machine code). ![]() In this article, we are going to look at what stack and heap memory are, their key differences, and where your variables are stored when they are in memory.Īt a high level, the memory for your application is laid out like this: If you are programming in C or C++ then understanding heap memory and stack memory is going to be essential knowledge. Understanding how the underlying memory management works as well as where our variables are stored can still be really helpful when it comes to understanding the scope of our variables. Luckily a lot of that is done for us by the runtimes so we don’t usually need to allocate and deallocate memory. ![]() Gone are the days when we need to call malloc to request enough memory for our variables. ![]() In modern programming languages such as C# or Java, we tend to take memory management for granted. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |