This document discusses the differences between the stack and heap in computer memory. The stack stores function parameters and local variables sequentially and follows LIFO order. The heap is reserved for dynamic memory allocation using malloc() and freed using free(). The stack has faster access but limited size, while the heap has no size limit but slower access and requires manual memory management. Examples are provided to illustrate how variables are stored in each.
3. Disclaimer: This presentation is prepared by trainees of
baabtra as a part of mentoring program. This is not official
document of baabtra –Mentoring Partner
Baabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt . Ltd
4. • Stores a set of elements in a sequential order
• Stack principle: LAST IN FIRST OUT (LIFO)
• It means: the last element inserted, is the first one to be
removed
Which is the first element to pick up?
The Stack
Example
5. • When a function declares a new variable, it is
pushed onto the stack.
• And when it exits, all of the variables pushed by
that function, are freed (means deleted).
• So that region of memory becomes available for
other stack variables.
Stack in functions
6. public int factorial (int x)
{
if (x > 1)
{
//recursive case:
return factorial(x-1) * x;
}
else /*base case*/
return 1;
}
Factorial(3){
If(3>1){
return factorial (2)*3
Factorial(2){
If(2>1){
return factorial (1)*2
Factorial(1){
If(1>1){
return 1;
Example of Stack in recursive function
1
2
Final output: 6
7. Example for Stack
#include <stdio.h>
double multiplyByTwo (double input) {
double twice = input * 2.0;
return twice;
}
int main (int argc, char *argv[])
{
int age = 30;
double salary = 12345.67;
double myList[3] = {1.2, 2.3, 3.4};
printf("double your salary is %.3fn",
multiplyByTwo(salary));
return 0;
}
8. The Heap
• The heap is an area of memory reserved for dynamic
memory allocation.
• Dynamic memory can be allotted by calling the malloc( )
function in C.
• When this memory is no longer needed it can be freed
up by calling free( ) .
• Once the memory is freed it can reused by future
allocations.
• The location and size of the heap are set at compile
time.
11. Stack
• very fast access
• don't have to explicitly de-allocate variables
• space is managed efficiently by CPU, memory
will not become fragmented
• local variables only
• limit on stack size (OS-dependent)
• variables cannot be resized
Stack vs Heap Pros and Cons
12. Heap
• variables can be accessed globally
• no limit on memory size
• (relatively) slower access
• no guaranteed efficient use of space, memory may
become fragmented over time as blocks of memory
are allocated, then freed
• you must manage memory (you're in charge of
allocating and freeing variables)
• variables can be resized using realloc()
Stack vs Heap Pros and Cons
15. Want to learn more about programming or Looking to become a good programmer?
Are you wasting time on searching so many contents online?
Do you want to learn things quickly?
Tired of spending huge amount of money to become a Software professional?
Do an online course
@ baabtra.com
We put industry standards to practice. Our structured, activity based courses are so designed
to make a quick, good software professional out of anybody who holds a passion for coding.
16. Follow us @ twitter.com/baabtra
Like us @ facebook.com/baabtra
Subscribe to us @ youtube.com/baabtra
Become a follower @ slideshare.net/BaabtraMentoringPartner
Connect to us @ in.linkedin.com/in/baabtra
Give a feedback @ massbaab.com/baabtra
Thanks in advance
www.baabtra.com | www.massbaab.com |www.baabte.com
17. Emarald Mall (Big Bazar Building)
Mavoor Road, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550
NC Complex, Near Bus Stand
Mukkam, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550
Cafit Square,
Hilite Business Park,
Near Pantheerankavu,
Kozhikode
Start up Village
Eranakulam,
Kerala, India.
Email: info@baabtra.com
Contact Us