Functions with heap and stack

284 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
284
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
13
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Functions with heap and stack

  1. 1. 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
  2. 2. 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
  3. 3. • 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
  4. 4. • 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
  5. 5. 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
  6. 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; }
  7. 7. 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.
  8. 8. 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; }
  9. 9. Both programs will show output as : double your salary is 24691.340
  10. 10. 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
  11. 11. 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
  12. 12. Doubts…?
  13. 13. thank you
  14. 14. 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.
  15. 15. 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
  16. 16. 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

×