Stack and Heap
Stack and Heap
What is Heap?
The heap is a memory used by programming languages to store global
variables. By default, all global variable are stored in heap memory space. It
supports Dynamic memory allocation.
The heap is not managed automatically for you and is not as tightly managed
by the CPU. It is more like a free-floating region of memory
KEY DIFFERENCE
Stack is a linear data structure whereas Heap is a hierarchical data
structure.
Stack memory will never become fragmented whereas Heap memory
can become fragmented as blocks of memory are first allocated and
then freed.
Stack accesses local variables only while Heap allows you to access
variables globally.
Stack variables can’t be resized whereas Heap variables can be
resized.
Stack memory is allocated in a contiguous block whereas Heap memory
is allocated in any random order.
Stack doesn’t require to de-allocate variables whereas in Heap de-
allocation is needed.
Stack allocation and deallocation are done by compiler instructions
whereas Heap allocation and deallocation is done by the programmer.
Key Differences between Stack and Heap
Parameter Stack Heap
Type of data A stack is a linear data Heap is a hierarchical data
structures structure. structure.
Access speed High-speed access Slower compared to stack
Heap Space not used as efficiently.
Space managed efficiently by
Space Memory can become fragmented
OS so memory will never
management as blocks of memory first allocated
become fragmented.
and then freed.
It allows you to access variables
Access Local variables only
globally.
Limit of space Limit on stack size dependent Does not have a specific limit on
size on OS. memory size.
Resize Variables cannot be resized Variables can be resized.
Memory Memory is allocated in a Memory is allocated in any random
Allocation contiguous block. order.
Allocation and Automatically done by compiler It is manually done by the
Deallocation instructions. programmer.
Does not require to de-allocate
Deallocation Explicit de-allocation is needed.
variables.
Cost Less More
A stack can be implemented in
3 ways simple array based, Heap can be implemented using
Implementation
using dynamic memory, and array and trees.
Linked list based.
Main Issue Shortage of memory Memory fragmentation
Locality of Automatic compile time
Adequate
reference instructions.
Flexibility Fixed size Resizing is possible
Access time Faster Slower
Advantages of using Stack
Helps you to manage the data in a Last In First Out(LIFO) method which
is not possible with Linked list and array.
When a function is called the local variables are stored in a stack, and it
is automatically destroyed once returned.
A stack is used when a variable is not used outside that function.
It allows you to control how memory is allocated and deallocated.
Stack automatically cleans up the object.
Not easily corrupted
Variables cannot be resized.
However, If you are working with relatively small variables that are only
required until the function using them is alive. Then you need to use the stack,
which is faster and easier.
# SQL Injection
# Command Injection
# XXE
# XSS
# CSRF
# CORS
# SSRF
# CLRF
# LFI/RFI
# LFD
# DOS
# DDOS
# IDOR
# WAF Bypass
course@pentestsage:~$
# HTTP Smuggling
# Cache Poisoning
# Broken Object Level Authorization
# Broken User Authentication
# Excessive Data Exposure
# Mass Assignment
# Improper Assets Management
# Security Misconfiguration
# Broken Function Level Authorization
# Lack of Resources & Rate Limiting
# Session Hijacking
course@pentestsage:~$
# Android app reversing basic
# Testing Android App
# Testing Hybrid App
# Other Surprise Testing
course@pentestsage:~$
# Burp
# Metaspoit
# Sqlmap
# other public and private tools