Your SlideShare is downloading. ×
0
Functions using stack and heap
Functions using stack and heap
Functions using stack and heap
Functions using stack and heap
Functions using stack and heap
Functions using stack and heap
Functions using stack and heap
Functions using stack and heap
Functions using stack and heap
Functions using stack and heap
Functions using stack and heap
Functions using stack and heap
Functions using stack and heap
Functions using stack and heap
Functions using stack and heap
Functions using stack and heap
Functions using stack and heap
Functions using stack and heap
Functions using stack and heap
Functions using stack and heap
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 using stack and heap

193

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
193
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
15
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. Sreyachandran.v Sreya.chandran31@gmail.com www.facebook.com/sreya twitter.com/username in.linkedin.com/in/profilename Stack and Heap
  • 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. 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.
  • 4. Push and Pop • Writing a value to the stack is called push. • Deleting a value from stack is called pop.
  • 5. #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; }
  • 6. 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
  • 7. 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
  • 8. 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);
  • 9. #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
  • 10. 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);
  • 11. #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
  • 12. 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);
  • 13. 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
  • 14. #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
  • 15. 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.
  • 16. 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.
  • 17. 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
  • 18. 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

×