What is heap and stack?

The stack is a place in the computer memory where all the variables that are declared and initializedbefore runtime are stored. The heap is the section of computer memory where all the variables created or initializedat runtime are stored.

What are the memory segments?

The distinction between stack and heap relates to programming. When you look at your computer memory, it is organized into three segments:

  • text (code) segment
  • stack segment
  • heap segment

The text segment (often called code segment) is where the compiled code of the program itself resides. When you open some EXE file in Notepad, you can see that it includes a lot of "Gibberish" language, something that is not readable to human. It is the machine code, the computer representation of the program instructions. This includes all user defined as well as system functions.

Heap and stack - what is it?

Now let's get to some details.

What is stack?

The two sections other from the code segment in the memory are used for data. The stack is the section of memory that is allocated forautomatic variables within functions.

Data is stored in stack using the Last In First Out (LIFO) method. This means that storage in the memory is allocated and deallocated at only one end of the memory called thetop of the stack. Stack is a section of memory and its associated registers that is used for temporary storage of information in which the most recently stored item is the first to be retrieved.

What is heap?

On the other hand, heap is an area of memory used fordynamic memory allocation. Blocks of memory are allocated and freed in this case in an arbitrary order. The pattern of allocation and size of blocks is not known until run time. Heap is usually being used by a program for many different purposes.

The stack is much faster than the heap but also smaller and more expensive.

Heap and stack from programming perspective

Most object-oriented languages have some defined structure, and some come with so-calledmain() function. When a program begins running, the system calls the functionmain() which marks the entry point of the program. For example every C, C++, or C# program must have one function namedmain(). No other function in the program can be calledmain(). Before we start explaining, let's take a look at the following example:

int x;                           /* static stack storage */
void main() {
   int y;                        /* dynamic stack storage */
   char str;                    /* dynamic stack storage */
   str = malloc(50);        /* allocates 50 bytes of dynamic heap storage */
   size = calcSize(10);       /* dynamic heap storage */

When a program begins executing in the main() function, all variables declared within main() will be stored on the stack.

If the main() function calls another function in the program, for examplecalcSize(), additional storage will be allocated for the variables incalcSize(). This storage will be allocated in theheap memory segment.

Notice that the parameters passed by main() to calcSize() are also stored on the stack. If thecalcSize() function calls to any additional functions, more space would be allocated at the heap again.

When the calcSize() function returns the value, the space for its local variables at heap is then deallocated and heap clears to be available for other functions.

The memory allocated in the heap area is used and reused during program execution.

It should be noted that memory allocated in heap will contain garbage values left over from previous usage.

Memory space for objects is always allocated in heap. Objects are placed on the heap.

Built-in datatypes like int, double, float and parameters to methods are allocated on the stack.

Even though objects are held on heap, references to them are also variables and they are placed on stack.

The stack segment provides more stable storage of data for a program. The memory allocated in the stack remains in existence for the duration of a program. This is good for global and static variables. Therefore, global variables and static variables are allocated on the stack.

Why is stack and heap important?

When a program is loaded into memory, it takes some memory management to organize the process. If memory management was not present in your computer memory, programs would clash with each other leaving the computer non-functional.

Heap and stack in Java

When you create an object using the new operator, for example myobj = new Object();, it allocates memory for the myobj object on theheap. The stack memory space is used when you declare automatic variables.

Note, when you do a string initialization, for example String myString;, it is a reference to an object so it will be created usingnew and hence it will be placed on the heap.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值