SlideShare a Scribd company logo
Functions using stack and heap
Sreyachandran.v
Sreya.chandran31@gmail.com
www.facebook.com/sreya
twitter.com/username
in.linkedin.com/in/profilename
Stack and Heap
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
Stack
• Stack is a linear data structure in which a data item is
inserted and deleted at one record.
• A stack is called LIFO(Last In First Out) or
FILO(First In Last Out) structure.
• The first item added to a stack is the last item
removed from a stack.
• Compiler store the local variable inside the stack.
Push and Pop
• Writing a value to the stack is called push.
• Deleting a value from stack is called pop.
#include <stdio.h>
int hcf(int n1, int n2);
int main()
{
int n1, n2;
n1=24;
n2=16;
printf("H.C.F of %d and %d = %d", n1, n2, hcf(n1,n2));
return 0;
}
int hcf(int n1, int n2)
{
i f(n2!=0)
return hcf(n2, n1%n2);
else
return n1;
}
1.main()
n1=24
n2=16
2.hcf(24,16)
3.hcf(16,24%16)=hcf(16,8)
4.hcf(8,16%8)=hcf(8,0)n1=24
n2=16
hcf(24,16)
hcf(16,8)
hcf(8,0)
STACK
Representation of stack operation
Heap
• The heap is an area of memory reserved for dynamic
memory allocation.
• Dynamic allocation is a technique in which a program
can acquire storage space in main memory.
• In this method , the space for the program is allocated
from the free space during execution of the program.
• This free space is called Heap
Memory allocation functions
1. malloc()
This function is used to allocate memory space in
bytes to the variable of different data types.
Example: ptr = (int*)malloc(20);
Ptr=(data type*)malloc(given size);
#include<stdio.h>
#include<stdlib.h>
main()
{
int k,*p,j=0,sum=0;
printf("How many numbers:");
scanf("%d",&k);
p=(int*)malloc(k*sizeof(int));
printf("n enter the numbers");
while(j!=k)
{
scanf("%d", p+j);
j++;
}
j=0;
printf("sum: ");
while(j!=k)
{
sum=sum+*(p+j);
j++;
}
printf("%d",sum);
}
How many numbers:3
enter the numbers: 1 2 3
Sum: 6
2. calloc()
• This function is useful for allocating multiple blocks
of memory . It is declared with two arguments.
• It allocate 4 blocks of memory and each block
contain 2 bytes
• This function is usually used for allocating memory
for array and structure
Ptr =(int*)calloc(4,2);
#include<stdio.h>
#include<stdlib.h>
main()
{
int k,*p,j=0,sum=0;
printf("How many numbers:");
scanf("%d",&k);
p=(int*)calloc(k,2);
printf("nenter the numbers");
while(j!=k)
{
scanf("%d",p+j);
j++;
}
j=0;
printf("sum: ");
while(j!=k)
{
sum=sum+*(p+j);
j++;
}
printf("%d",sum);
}
How many numbers:3
enter the numbers: 1 2 3
Sum: 6
3.free()
• This function is used to release the memory allocated
by memory allocating functions.
• ptr is the pointer and free() releases the memory
occupied by the pointer variable.
free(ptr);
4.realloc()
• This function reallocate the main memory.
• This are made to shrink or enlarge the previously
allocated memory.
• It returns the address of the reallocated block.
• If the block cannot be reallocated returns NULL
#include<stdio.h>
#include<stdlib.h>
main()
{
char *str;
str=(char*)malloc(6);
str=("india");
printf("str=%s",str);
str=(char*)realloc(str,10);
str=("hindustan");
printf("nnew str=%s",str);
free(str);
}
str=india
new str=hindustan
Stack vs Heap
stack
• local variables only.
• limit on stack size.
• variables cannot be resized.
• space is managed efficiently
by CPU, memory will not
become fragmented .
heap
• variables can be accessed
globally .
• no limit on memory size.
• variables can be resized
using realloc() .
• no guarantee on efficient use
of space.
Functions using stack and heap
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
PDF
Lecture 5
PDF
Standardizing on a single N-dimensional array API for Python
PDF
Pycon tw 2013
PDF
Data visualization in Python
PPTX
F# Ignite - DNAD2010
PPTX
Apriori algorithm
PDF
Neat Analytics with Pandas Indexes, Alexander Hendorf
Lecture 5
Standardizing on a single N-dimensional array API for Python
Pycon tw 2013
Data visualization in Python
F# Ignite - DNAD2010
Apriori algorithm
Neat Analytics with Pandas Indexes, Alexander Hendorf

Viewers also liked (20)

ODP
Open Power Template 2 presentation
PPTX
What is xml
PPT
PPT
XML Training Presentation
PPT
XML and Databases
PDF
Intro to JSON
PDF
XML 101 presentation by Bill Kasdorf of Apex
PPTX
Chapter 5 transactions and dcl statements
PDF
Field Service Forum 2016
PDF
I-Gaming Forum 2015 Post Event Report
PPTX
Cpu and execution of instruction.
PDF
Meet Nordic IT Security Advisory board, David Jacoby
PDF
TOP5 facts about Mobile Payments
Open Power Template 2 presentation
What is xml
XML Training Presentation
XML and Databases
Intro to JSON
XML 101 presentation by Bill Kasdorf of Apex
Chapter 5 transactions and dcl statements
Field Service Forum 2016
I-Gaming Forum 2015 Post Event Report
Cpu and execution of instruction.
Meet Nordic IT Security Advisory board, David Jacoby
TOP5 facts about Mobile Payments
Ad

Similar to Functions using stack and heap (20)

PPTX
PPTX
Stack & heap
PPTX
PPTX
Lecture 3.3.1 Dynamic Memory Allocation and Functions.pptx
PDF
PPTX
Memory Management.pptx
PPTX
Functions with heap and stack....by thanveer danish
PPTX
C dynamic ppt
PPTX
data structure notes for engineering DSA3.pptx
PPTX
Structures_and_Files[1] - Read-Only.pptx
PDF
dynamic-allocation.pdf
PPTX
Dynamic Memory Allocation in C
PPTX
Functions with Heap and stack
PPT
C language introduction
Stack & heap
Lecture 3.3.1 Dynamic Memory Allocation and Functions.pptx
Memory Management.pptx
Functions with heap and stack....by thanveer danish
C dynamic ppt
data structure notes for engineering DSA3.pptx
Structures_and_Files[1] - Read-Only.pptx
dynamic-allocation.pdf
Dynamic Memory Allocation in C
Functions with Heap and stack
C language introduction
Ad

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 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 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)

PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
Cloud computing and distributed systems.
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Empathic Computing: Creating Shared Understanding
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Electronic commerce courselecture one. Pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
A Presentation on Artificial Intelligence
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Approach and Philosophy of On baking technology
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PPTX
Understanding_Digital_Forensics_Presentation.pptx
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Cloud computing and distributed systems.
Spectral efficient network and resource selection model in 5G networks
Empathic Computing: Creating Shared Understanding
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Dropbox Q2 2025 Financial Results & Investor Presentation
Agricultural_Statistics_at_a_Glance_2022_0.pdf
“AI and Expert System Decision Support & Business Intelligence Systems”
Digital-Transformation-Roadmap-for-Companies.pptx
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Advanced methodologies resolving dimensionality complications for autism neur...
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Electronic commerce courselecture one. Pdf
Network Security Unit 5.pdf for BCA BBA.
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
A Presentation on Artificial Intelligence
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Approach and Philosophy of On baking technology
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Understanding_Digital_Forensics_Presentation.pptx

Functions using stack and heap

  • 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. Stack • Stack is a linear data structure in which a data item is inserted and deleted at one record. • A stack is called LIFO(Last In First Out) or FILO(First In Last Out) structure. • The first item added to a stack is the last item removed from a stack. • Compiler store the local variable inside the stack.
  • 5. Push and Pop • Writing a value to the stack is called push. • Deleting a value from stack is called pop.
  • 6. #include <stdio.h> int hcf(int n1, int n2); int main() { int n1, n2; n1=24; n2=16; printf("H.C.F of %d and %d = %d", n1, n2, hcf(n1,n2)); return 0; } int hcf(int n1, int n2) { i f(n2!=0) return hcf(n2, n1%n2); else return n1; }
  • 8. Heap • The heap is an area of memory reserved for dynamic memory allocation. • Dynamic allocation is a technique in which a program can acquire storage space in main memory. • In this method , the space for the program is allocated from the free space during execution of the program. • This free space is called Heap
  • 9. Memory allocation functions 1. malloc() This function is used to allocate memory space in bytes to the variable of different data types. Example: ptr = (int*)malloc(20); Ptr=(data type*)malloc(given size);
  • 10. #include<stdio.h> #include<stdlib.h> main() { int k,*p,j=0,sum=0; printf("How many numbers:"); scanf("%d",&k); p=(int*)malloc(k*sizeof(int)); printf("n enter the numbers"); while(j!=k) { scanf("%d", p+j); j++; } j=0; printf("sum: "); while(j!=k) { sum=sum+*(p+j); j++; } printf("%d",sum); } How many numbers:3 enter the numbers: 1 2 3 Sum: 6
  • 11. 2. calloc() • This function is useful for allocating multiple blocks of memory . It is declared with two arguments. • It allocate 4 blocks of memory and each block contain 2 bytes • This function is usually used for allocating memory for array and structure Ptr =(int*)calloc(4,2);
  • 12. #include<stdio.h> #include<stdlib.h> main() { int k,*p,j=0,sum=0; printf("How many numbers:"); scanf("%d",&k); p=(int*)calloc(k,2); printf("nenter the numbers"); while(j!=k) { scanf("%d",p+j); j++; } j=0; printf("sum: "); while(j!=k) { sum=sum+*(p+j); j++; } printf("%d",sum); } How many numbers:3 enter the numbers: 1 2 3 Sum: 6
  • 13. 3.free() • This function is used to release the memory allocated by memory allocating functions. • ptr is the pointer and free() releases the memory occupied by the pointer variable. free(ptr);
  • 14. 4.realloc() • This function reallocate the main memory. • This are made to shrink or enlarge the previously allocated memory. • It returns the address of the reallocated block. • If the block cannot be reallocated returns NULL
  • 16. Stack vs Heap stack • local variables only. • limit on stack size. • variables cannot be resized. • space is managed efficiently by CPU, memory will not become fragmented . heap • variables can be accessed globally . • no limit on memory size. • variables can be resized using realloc() . • no guarantee on efficient use of space.
  • 18. 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.
  • 19. 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
  • 20. 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: [email protected] Contact Us