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
Stack and Heap


        Jithin Mathew
        jitmat@gmail.com
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.
• Stacks in computing architectures are regions
  of memory where data is added or removed in
  a last-in-first-out manner.
• 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.
• 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
• 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.
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.
• 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.
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.
Stack and Heap Examples
public void Method1()
{
  int i=4;
  int y=2;
  class1 cls1 = new class1();
}
• 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.
• 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.
• 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.
• Did this presentation help you??? Do visit our
  page www.facebook.com/baabtra and like
  us

www.baabtra.com
www.massbaab.com
www.baabte.com
Thank you

Stack and heap

  • 2.
    Disclaimer: This presentationis 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 and Heap Jithin Mathew jitmat@gmail.com
  • 4.
    Stack • The stackis 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.
  • 5.
    • Stacks incomputing architectures are regions of memory where data is added or removed in a last-in-first-out manner.
  • 6.
    • The stackhas 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.
  • 7.
    • Example ForStack 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
  • 9.
    • This programtries 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.
  • 10.
    Heap • On theother 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.
  • 11.
    • Any dataon 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.
  • 12.
    Which is morefast? • The stack is much faster than the heap. • This is because of the way that memory is allocated on the stack.
  • 13.
    Stack and HeapExamples public void Method1() { int i=4; int y=2; class1 cls1 = new class1(); }
  • 14.
    • A classis 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.
  • 16.
    • When Firstline 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.
  • 17.
    • When thirdline 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.
  • 18.
    • Did thispresentation help you??? Do visit our page www.facebook.com/baabtra and like us www.baabtra.com www.massbaab.com www.baabte.com
  • 19.