Stack and heap


Published on

  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Stack and heap

  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 Heap Jithin Mathew
  3. 3. Stack• The stack is a place in the computer memory where all the variables that are declared and initialized before runtime are stored.• It is a temporary storage memory, ,if you come out of the program the memory of the variable will not no more there.• Any data on the stack for a function will automatically be deleted.
  4. 4. • Stacks in computing architectures are regions of memory where data is added or removed in a last-in-first-out manner.
  5. 5. • The stack has a fixed size.• Both stack and heap are store on the RAM• If there is not enough space on the stack to handle the memory being assigned to it, a stack overflow occurs.
  6. 6. • Example For Stack overflow #include <stdio.h> int main() { int nStack[100000000]; return 0; }• This program that causes a stack overflow.• If you run this the program will crash
  7. 7. • This program tries to allocate a huge array on the stack.• Because the stack is not large enough to handle this array, the array allocation overflows into portions of memory the program is not allowed to use.• Consequently, the program crashes.
  8. 8. Heap• On the other hand, heap is an area of memory used for dynamic memory allocation.• A place for allocating memory that is not part of last-in, first-out discipline.• This used to store global variables.• Variables on the heap must be destroyed manually.
  9. 9. • Any data on the heap will remain there until it’s manually deleted by the programmer.• If the current size of the heap is too small to accommodate new memory, then more memory can be added to the heap by the operating system.
  10. 10. Which is more fast?• The stack is much faster than the heap.• This is because of the way that memory is allocated on the stack.
  11. 11. Stack and Heap Examplespublic void Method1(){ int i=4; int y=2; class1 cls1 = new class1();}
  12. 12. • A class is just a template which contains the various attributes and functions.• Objects are the building blocks of OOP and are commonly defined as variables or data structures that encapsulate behavior in a programmed unit.
  13. 13. • When First line is executed, the compiler allocates a small amount of memory in the stack.• Then it stacks this memory allocation on top of the first memory allocation.
  14. 14. • When third line is executed it creates a pointer on the stack and the actual object is stored in a different type of memory location• When it passes the end control, it clears all the memory variables which are assigned on stack.
  15. 15. • Did this presentation help you??? Do visit our page and like
  16. 16. Thank you