Functions with Heap and stack

356 views

Published on

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

  • Be the first to like this

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

No notes for slide

Functions with Heap and stack

  1. 1. Disclaimer: This presentation is prepared by trainees ofbaabtra as a part of mentoring program. This is not officialdocument of baabtra –Mentoring PartnerBaabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt .Ltd
  2. 2. STACK AND HEAPARJUN S RArjun.sr007@gmail.comwww.facebook.com/arjun.aryavarthamtwitter.com/arjun.sr007in.linkedin.com/in/arjun.sr0079745649882
  3. 3. PROGRAM ADDRESS SPACE• Any program you run has, associated with it, somememory which is divided into:– Code Segment– Data Segment (Holds Global Data)– Stack (where the local variables and other temporary information isstored)– Heap
  4. 4. Continue…..Code SegmentData SegmentStackHeapThe Heap grows downwardsThe Stack grows upwards
  5. 5. STACK• Stack – a linear data structure in which items are added andremoved in last-in, first-out order.• In most modern computer systems, each thread has areserved region of memory referred to as its stack.• When a function executes, it may add some of its state data tothe top of the stack; when the function exits it is responsiblefor removing that data from the stack.• The place where arguments of a function call are stored• The place where local data of called function is allocated• Automatic data
  6. 6. • The place where called function leaves result for callingfunction(return address).• Supports recursive function calls.• All modern programming languages require a stack.• All modern processors provide a designated stack pointerregister.• All modern process address spaces provide room for a stack• It has mainly two operationspush-process of insert values into stackpop –Process of remove a value from the stack
  7. 7. Process Address Spaces0x000000000xFFFFFFFFVirtualaddress spaceprogram code(text)static dataheap(dynamically allocated)stack(dynamically allocated)PCSP
  8. 8. HEAP• The heap is used to store variables dynamicallyallocated by the application (malloc). The datasection initialized at compile-time.• Dynamic variables are never initialized by thecompiler, so it is a good practice to initialize it.Eg:int *p;p=new int;*p=0;
  9. 9. • A place for allocating memory that is not part of last-in, first-out discipline• I.e., dynamically allocated data structures thatsurvive function calls• E.g., strings in C• new objects in C++, Java, etc.• The size of the heap is set on application startup, butcan grow as space is needed (the allocator requestsmore memory from the operating system)• Used on demand to allocate a block of data for useby the program.
  10. 10. HEAP AND STACK EXAMPLEint main() {pointT pt;double total = 0.0;pt.x = 1;pt.y = 2;int *array = new int[5];Nonsense(array, pt, total);return 0;}void Nonsense(int list[], pointT pt, double & sum) {pointT *pptr = new pointT;list[1] = pt.x;sum += pt.y;}
  11. 11. HEAP stack1000 pptr FFDO1004 sum FFD41008 pt FFD8100C list FFE010101014 array FFE81018 total FFECFFF0pt FFF4FFF811004FFEC12100010002.012
  12. 12. If this presentation helped you, please visit ourpage facebook.com/baabtra and like it.Thanks in advance.www.baabtra.com | www.massbaab.com |www.baabte.com
  13. 13. Contact UsEmarald Mall (Big Bazar Building)Mavoor Road, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550NC Complex, Near Bus StandMukkam, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550Start up VillageEranakulam,Kerala, India.Email: info@baabtra.com

×