
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Heap overflow and Stack overflow
Heap Overflow
Heap is used to store dynamic variables. It is a region of process’s memory. malloc(), calloc(), resize() all these inbuilt functions are generally used to store dynamic variables.
Heap overflow occurs when −
A) If we allocate dynamic large number of variables −
int main() { float *ptr = (int *)malloc(sizeof(float)*1000000.0)); }
B) If we continuously allocate memory and do not free after using it.
int main() { for (int i=0; i<100000000000; i++) { int *p = (int *)malloc(sizeof(int)); } }
Stack Overflow
The stack is a Last in First out data structure. It is used to store local variables which is used inside the function. Parameters are passed through this function and their return addresses.
If a program consumes more memory space, then stack overflow will occur as stack size is limited in computer memory.
Stack overflow occurs when
A) If a function is called recursively by itself infinite times then stack will be unable to store large number of local variables, so stack overflow will occur −
void calculate(int a) { if (a== 0) return; a = 6; calculate(a); } int main() { int a = 5; calculate(a); }
B) If we declare a large number of local variables or declare a large dimensional array or matrix can result in stack overflow.
int main() { A[20000][20000]; }
- Related Articles
- Heap overflow and Stack overflow in C
- Java overflow and underflow
- Counter Size and Counter Overflow
- CSS overflow: visible
- CSS overflow: hidden
- CSS overflow: scroll
- CSS overflow: auto
- CSS overflow-y
- CSS overflow-x
- CSS text-overflow property
- Values of CSS overflow property
- Overflow of DataTypes in Java
- HTML DOM Style overflow Property
- Working with CSS Overflow Property
- Overflow Handling in Data Structure
