Your SlideShare is downloading. ×
Functions with heap and stack
Upcoming SlideShare
Loading in...5
×

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.
Text the download link to your phone
Standard text messaging rates apply

Functions with heap and stack

210
views

Published on

Published in: Technology, Business

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

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

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
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
  • 11. If this presentation helped you, please visit our page facebook.com/baabtra and like it. Thanks in advance. www.baabtra.com | www.massbaab.com |www.baabte.com
  • 12. Contact Us 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 Start up Village Eranakulam, Kerala, India. Email: info@baabtra.com IKK Road, East Hill, Kozhikode Kerala, India. Ph: + 91 – 495 30 63 624 NIT-TBI, NIT Campus, Kozhikode, Kerala, India.

×