Download - Functions with heap and stack
Mohammed Razi Krazikallayi @gmail.comwww.facebook.com/
razikallayitwitter.com/razikallayiin.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 PartnerBaabtra-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 %.3f\n", 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 %.3f\n", *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 StandMukkam, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550
Cafit Square,Hilite Business Park,Near Pantheerankavu,Kozhikode
Start up VillageEranakulam,Kerala, India.Email: [email protected]
Contact Us