- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Heap overflow and Stack overflow in C
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
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 −
C) 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); }
D) If we declare a large number of local variables or declare a large dimensional array or matrix can result stack overflow.
int main() { A[20000][20000] }
- Related Articles
- Heap overflow and Stack overflow
- Java overflow and underflow
- Check for Integer Overflow in C++
- Counter Size and Counter Overflow
- CSS overflow: visible
- CSS overflow: hidden
- CSS overflow: scroll
- CSS overflow: auto
- CSS overflow-y
- CSS overflow-x
- How to detect integer overflow in C++?
- How to detect integer overflow in C/C++?
- Proper stack and heap usage in C++?
- Overflow of DataTypes in Java
- Overflow Handling in Data Structure
