Upcoming SlideShare
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Standard text messaging rates apply

# Functions with heap and stack

210
views

Published on

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total Views
210
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
7
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Transcript

• 1. 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
• 2. Week Target Achieved 1 30 20 2 3 Typing Speed
• 3. Functions with heap and stack Yasir musthafa pp yasirmusthafapp@gmail.com www.facebook.com/yasirmu sthafa pp twitter.com/yasirmusthafa pp in.linkedin.com/in/yasirmus thafapp 8891396749
• 4. Stack • special region of our computer's memory that stores temporary variables created by each function (including the main() function) • "FILO" (first in, last out) data structure- managed by CPU • Every time a function declares a new variable- pushed &popped
• 5. Stack (conti) • variables are allocated and freed automatically • stack has size limits • stack variables are local in nature • stack variables is very fast • variables cannot be resized
• 6. Example #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; printf("double your salary is %.3fn", multiplyByTwo(salary)); return 0; } output double your salary is 24691.340
• 7. Heap • region of our computer's memory that is not managed automatically • To allocate memory - malloc() or calloc() • To delete memory – free() • Memory leak
• 8. Heap(conti) • variables can be accessed globally • no limit on memory size • (relatively) slower access • you must manage memory (you're in charge of allocating and freeing variables) • variables can be resized using realloc()
• 9. Example #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 *twiceSalary = multiplyByTwo(salary); printf("double your salary is %.3fn", *twiceSalary); free(age); free(salary); free(twiceSalary); return 0; }
• 10. When to use the Heap? • you are dealing with relatively small variables –stack • you need to allocate a large block of memory - heap