SlideShare a Scribd company logo
Stack and heap
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
Stack and heap
• 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.
Stack and heap
• 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

More Related Content

PPTX
Functions with Heap and stack
PPTX
AI4Media WP3 workshop - Distributed training introduction
PPT
Ch22 parallel d_bs_cs561
DOCX
computer fundamentals unit 3 notes
PDF
Autoencoder Forest for Anomaly Detection from IoT Time Series
PPTX
MALT: Distributed Data-Parallelism for Existing ML Applications (Distributed ...
PPTX
Energy Monitoring With Self-taught Deep Network
PPTX
Map reduce advantages over parallel databases
Functions with Heap and stack
AI4Media WP3 workshop - Distributed training introduction
Ch22 parallel d_bs_cs561
computer fundamentals unit 3 notes
Autoencoder Forest for Anomaly Detection from IoT Time Series
MALT: Distributed Data-Parallelism for Existing ML Applications (Distributed ...
Energy Monitoring With Self-taught Deep Network
Map reduce advantages over parallel databases
Ad

Similar to Stack and heap (20)

PPTX
PPTX
PDF
PPTX
Functions using stack and heap
PPTX
PPTX
Functions with heap and stack....by thanveer danish
PPTX
Stack & heap
PPTX
C++ Memory Management
PDF
Dynamic memory allocation for data structure DS3a.pdf
PPTX
My lecture stack_queue_operation
PPTX
Lecture 15 run timeenvironment_2
PPTX
Run time administration
PDF
computer notes - Reference variables ii
Functions using stack and heap
Functions with heap and stack....by thanveer danish
Stack & heap
C++ Memory Management
Dynamic memory allocation for data structure DS3a.pdf
My lecture stack_queue_operation
Lecture 15 run timeenvironment_2
Run time administration
computer notes - Reference variables ii
Ad

More from baabtra.com - No. 1 supplier of quality freshers (20)

PPTX
Agile methodology and scrum development
PDF
Acquiring new skills what you should know
PDF
Baabtra.com programming at school
PDF
99LMS for Enterprises - LMS that you will love
PPTX
Chapter 6 database normalisation
PPTX
Chapter 5 transactions and dcl statements
PPTX
Chapter 4 functions, views, indexing
PPTX
PPTX
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
PPTX
Chapter 1 introduction to sql server
PPTX
Chapter 1 introduction to sql server
Agile methodology and scrum development
Acquiring new skills what you should know
Baabtra.com programming at school
99LMS for Enterprises - LMS that you will love
Chapter 6 database normalisation
Chapter 5 transactions and dcl statements
Chapter 4 functions, views, indexing
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
Chapter 1 introduction to sql server
Chapter 1 introduction to sql server

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 and Heap Jithin Mathew jitmat@gmail.com
  • 4. 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.
  • 5. • Stacks in computing architectures are regions of memory where data is added or removed in a last-in-first-out manner.
  • 6. • 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.
  • 7. • 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
  • 9. • 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.
  • 10. 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.
  • 11. • 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.
  • 12. 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.
  • 13. Stack and Heap Examples public void Method1() { int i=4; int y=2; class1 cls1 = new class1(); }
  • 14. • 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.
  • 16. • 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.
  • 17. • 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.
  • 18. • 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