---
title: "What is the difference between stack and heap?"  
description: "What is the difference between stack and heap?"  
author: "Shikhar Arora"  
published: 2019-12-07  
canonical: https://answers.mindstick.com/qa/92562/what-is-the-difference-between-stack-and-heap  
category: "technology"  
tags: [".net programming"]  
reading_time: 1 minute  

---

# What is the difference between stack and heap?

What is the [difference](https://www.mindstick.com/blog/398/difference-between-object-type-and-var-type) between [stack](https://www.mindstick.com/blog/301746/why-is-stack-overflow-so-important-for-developers) and [heap](https://www.mindstick.com/forum/159684/what-are-the-differences-between-heap-and-stack-memory-in-java)

## Answers

### Answer by Sangsaptak Pal

Stack is a special area of the [memory of the computer](https://en.wikipedia.org/wiki/Computer_memory) which is used to store temporary variables created by a function. In stack, the variables are declared, stored and initialized during [runtime](https://en.wikipedia.org/wiki/Runtime_(program_lifecycle_phase)). On the other hand, heap is a memory used by programming languages to store global variables. By default, all global variables are stored in heap memory space. Stack is a linear data structure whereas a heap is a hierarchical data structure. Stack cannot be resized but a heap can be resized. In stack, memory is allocated in a contiguous block only whereas in a heap, memory is allocated in any random order. The access time of stack is faster while that of the heap is slower. Stack comprises of local variables only but heap allows us to access variables globally. Stack allocation and deallocation is done automatically by [compiler](https://en.wikipedia.org/wiki/Compiler) instructions whereas the same thing needs to be done manually by a programmer in case of a heap. ![What is the difference between stack and heap?](https://upload.wikimedia.org/wikipedia/commons/thumb/b/b4/Lifo_stack.png/350px-Lifo_stack.png) Stack image ![What is the difference between stack and heap?](https://answers.mindstick.com/questionanswer/521007ba-bfb2-4a3f-8d61-5930987628bb/images/a71a7f99-54d8-4a75-a77b-95d4635bc614.png) Heap image


---

Original Source: https://answers.mindstick.com/qa/92562/what-is-the-difference-between-stack-and-heap

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
