SlideShare a Scribd company logo
Functions with heap and stack
MohammedRazi K
razikallayi@gmail.com
www.facebook.com/razikallayi
twitter.com/razikallayi
in.linkedin.com/in/razikallayi
+91 97467 30324
Functions With Heap And Stack
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
• 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
• 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
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
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;
}
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.
Example for Heap
#include <stdio.h>
#include <stdlib.h>
double *multiplyByTwo (double *input) {
double *twice = malloc(sizeof(double));
*twice = *input * 2.0;
return twice;
}
int main (int argc, char *argv[])
{
int *age = malloc(sizeof(int));
*age = 30;
double *salary = malloc(sizeof(double));
*salary = 12345.67;
double *myList = malloc(3 * sizeof(double));
myList[0] = 1.2;
myList[1] = 2.3;
myList[2] = 3.4;
double *twiceSalary = multiplyByTwo(salary);
printf("double your salary is %.3fn", *twiceSalary);
free(age);
free(salary);
free(myList);
free(twiceSalary);
return 0;
}
Both programs will show output as :
double your salary is 24691.340
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
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
Doubts…?
thank you
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.
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
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

More Related Content

PPTX
Functions using stack and heap
PPTX
PDF
Introducton to Convolutional Nerural Network with TensorFlow
PDF
Your first TensorFlow programming with Jupyter
PDF
Machine Learning Basics for Web Application Developers
PDF
Python + Tensorflow: how to earn money in the Stock Exchange with Deep Learni...
PDF
R programming lab 1 - jupyter notebook
PDF
Python Matplotlib Tutorial | Matplotlib Tutorial | Python Tutorial | Python T...
Functions using stack and heap
Introducton to Convolutional Nerural Network with TensorFlow
Your first TensorFlow programming with Jupyter
Machine Learning Basics for Web Application Developers
Python + Tensorflow: how to earn money in the Stock Exchange with Deep Learni...
R programming lab 1 - jupyter notebook
Python Matplotlib Tutorial | Matplotlib Tutorial | Python Tutorial | Python T...

What's hot (20)

PPTX
16 dynamic-memory-allocation
PPTX
Dynamic memory allocation in c
PPTX
Malloc() and calloc() in c
PDF
Bootcamp Code Notes - Akshansh Chaudhary
PPTX
[DevDay2019] Python Machine Learning with Jupyter Notebook - By Nguyen Huu Th...
PPTX
Introduction to matplotlib
PPTX
Bring your neural networks to the browser with TF.js - Simone Scardapane
PDF
Generative Adversarial Nets
PPTX
Dynamic Memory Allocation(DMA)
PDF
Introduction to TensorFlow, by Machine Learning at Berkeley
PPTX
Intoduction to dynamic memory allocation
PPTX
Memory allocation in c
PDF
Object detection and Instance Segmentation
PPTX
Tensor flow (1)
PDF
Introduction to TensorFlow
PPTX
Simone Scardapane - Bring your neural networks to the browser with TF.js! - C...
PPTX
Dynamic memory allocation
PPTX
Dynamic memory allocation(memory,allocation,memory allocatin,calloc,malloc,re...
PPT
Dynamic memory allocation
PDF
Data Visualization using matplotlib
16 dynamic-memory-allocation
Dynamic memory allocation in c
Malloc() and calloc() in c
Bootcamp Code Notes - Akshansh Chaudhary
[DevDay2019] Python Machine Learning with Jupyter Notebook - By Nguyen Huu Th...
Introduction to matplotlib
Bring your neural networks to the browser with TF.js - Simone Scardapane
Generative Adversarial Nets
Dynamic Memory Allocation(DMA)
Introduction to TensorFlow, by Machine Learning at Berkeley
Intoduction to dynamic memory allocation
Memory allocation in c
Object detection and Instance Segmentation
Tensor flow (1)
Introduction to TensorFlow
Simone Scardapane - Bring your neural networks to the browser with TF.js! - C...
Dynamic memory allocation
Dynamic memory allocation(memory,allocation,memory allocatin,calloc,malloc,re...
Dynamic memory allocation
Data Visualization using matplotlib
Ad

Viewers also liked (19)

Ad

Similar to Functions with heap and stack (20)

PPTX
PDF
PPTX
Functions with heap and stack....by thanveer danish
PPTX
Stack & heap
PPTX
Functions with Heap and stack
PDF
Dynamic memory allocation for data structure DS3a.pdf
PPTX
Lecture 3.3.1 Dynamic Memory Allocation and Functions.pptx
PDF
(5) cpp dynamic memory_arrays_and_c-strings
PDF
C interview questions
PDF
PDF
C interview-questions-techpreparation
PPT
Ctutorial-Pointers 1.ppt
PPT
Ctutorial-Pointers FULL AND DETAILED LEVEL OF EXPLANATION
PPT
C Programming Tutorial - Pointer Concepts
Functions with heap and stack....by thanveer danish
Stack & heap
Functions with Heap and stack
Dynamic memory allocation for data structure DS3a.pdf
Lecture 3.3.1 Dynamic Memory Allocation and Functions.pptx
(5) cpp dynamic memory_arrays_and_c-strings
C interview questions
C interview-questions-techpreparation
Ctutorial-Pointers 1.ppt
Ctutorial-Pointers FULL AND DETAILED LEVEL OF EXPLANATION
C Programming Tutorial - Pointer Concepts

More from baabtra.com - No. 1 supplier of quality freshers (20)

PPTX
Agile methodology and scrum development
PDF
Acquiring new skills what you should know
PDF
Baabtra.com programming at school
PDF
99LMS for Enterprises - LMS that you will love
PPTX
Chapter 6 database normalisation
PPTX
Chapter 5 transactions and dcl statements
PPTX
Chapter 4 functions, views, indexing
PPTX
PPTX
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
PPTX
Chapter 1 introduction to sql server
PPTX
Chapter 1 introduction to sql server
Agile methodology and scrum development
Acquiring new skills what you should know
Baabtra.com programming at school
99LMS for Enterprises - LMS that you will love
Chapter 6 database normalisation
Chapter 5 transactions and dcl statements
Chapter 4 functions, views, indexing
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
Chapter 1 introduction to sql server
Chapter 1 introduction to sql server

Recently uploaded (20)

PPTX
Big Data Technologies - Introduction.pptx
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPTX
Spectroscopy.pptx food analysis technology
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Spectral efficient network and resource selection model in 5G networks
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Electronic commerce courselecture one. Pdf
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
Big Data Technologies - Introduction.pptx
MIND Revenue Release Quarter 2 2025 Press Release
Spectroscopy.pptx food analysis technology
Chapter 3 Spatial Domain Image Processing.pdf
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Unlocking AI with Model Context Protocol (MCP)
Mobile App Security Testing_ A Comprehensive Guide.pdf
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Digital-Transformation-Roadmap-for-Companies.pptx
Encapsulation_ Review paper, used for researhc scholars
Spectral efficient network and resource selection model in 5G networks
“AI and Expert System Decision Support & Business Intelligence Systems”
Dropbox Q2 2025 Financial Results & Investor Presentation
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Electronic commerce courselecture one. Pdf
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
NewMind AI Weekly Chronicles - August'25 Week I
Network Security Unit 5.pdf for BCA BBA.
Advanced methodologies resolving dimensionality complications for autism neur...

Functions with heap and stack

  • 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.
  • 9. Example for Heap #include <stdio.h> #include <stdlib.h> double *multiplyByTwo (double *input) { double *twice = malloc(sizeof(double)); *twice = *input * 2.0; return twice; } int main (int argc, char *argv[]) { int *age = malloc(sizeof(int)); *age = 30; double *salary = malloc(sizeof(double)); *salary = 12345.67; double *myList = malloc(3 * sizeof(double)); myList[0] = 1.2; myList[1] = 2.3; myList[2] = 3.4; double *twiceSalary = multiplyByTwo(salary); printf("double your salary is %.3fn", *twiceSalary); free(age); free(salary); free(myList); free(twiceSalary); return 0; }
  • 10. Both programs will show output as : double your salary is 24691.340
  • 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