Stack and heap


Published on

Published in: Technology
  • 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
  • The heap, where dynamically allocated variables are allocated from.The stack, where parameters and local variables are allocated from.
  • Stack and heap

    1. 1. FUNCTIONS WITH STACK AND HEAP Anjali G Geetha Geetha G 9497879952
    2. 2. INTRODUCTION The memory a program uses is typically divided into four different areas: • code area • global area • heap • stack
    3. 3. STACK  stack is the memory set aside for thread execution The size of the stack is set when a thread is created Scope: attached to a thread, so when the thread exits the stack is reclaimed  When a function is called, a block is reserved on the top of the stack for local variables The stack is faster The stack is always reserved in a LIFO order. operations that can be performed on a stack : 1) Look at the top item on the stack (usually done via a function called top()) 2) Take the top item off of the stack (done via a function called pop()) 3) Put a new item on top of the stack (done via a function called push())  You would use the stack if you know exactly how much data you need to allocate before compile time and it is not too big.
    4. 4. The stack in action: 1)The address of the instruction beyond the function call is pushed onto the stack. This is how the CPU remembers where to go after the function returns. 2)Room is made on the stack for the function’s return type. 3)The CPU jumps to the function’s code. 4)The current top of the stack is held in a special pointer called the stack frame. Everything added to the stack after this point is considered “local” to the function. 5)All function arguments are placed on the stack. 6)The instructions inside of the function begin executing. 7)Local variables are pushed onto the stack as they are defined.
    5. 5. When the function terminates: 1)The function’s return value is copied into the placeholder that was put on the stack for this purpose. 2)Everything after the stack frame pointer is popped off. This destroys all local variables and arguments. 3)The return value is popped off the stack and is assigned as the value of the function. If the value of the function isn’t assigned to anything, no assignment takes place, and the value is lost. 4)The address of the next instruction to execute is popped off the stack, and the CPU resumes execution at that instruction.
    6. 6. #include <stdio.h> double multiplyByTwo (double input) { double twice = input * 2.0; return twice; } int main (int argc, char *argv[]) { int age = 30; double salary = 12345.67; double myList[3] = {1.2, 2.3, 3.4}; printf("double your salary is %.3fn", multiplyByTwo(salary)); return 0; }
    7. 7. Stack overflow: stack has a limited size If the program tries to put too much information on the stack, stack overflow will result Stack: advantages and disadvantages Memory allocated on the stack stays in scope as long as it is on the stack. It is destroyed when it is popped off the stack. All memory allocated on the stack is known at compile time. Consequently, this memory can be accessed directly through a variable. Because the stack is relatively small, it is generally not a good idea to do anything that eats up lots of stack space. This includes allocating large arrays, structures, and classes, as well as heavy recursion.
    8. 8. HEAP heap is memory set aside for dynamic allocation The size of the heap is set on application startup The heap is typically allocated at application startup by the runtime, and is reclaimed when the application exits. The size of the heap is set on application startup, but can grow as space is needed Variables on the heap must be destroyed manually and never fall out of scope. The data is freed with delete, delete[] or free Can have fragmentation when there are a lot of allocations and deallocations Used on demand to allocate a block of data for use by the program. You would use the heap if you don't know exactly how much data you will need at runtime or if you need to allocate a lot of data.
    9. 9. Code to create an object on the heap: void somefunction( ) { /* create an object "m" of class Member this will be put on the heap since the "new" keyword is used, and we are creating the object inside a function */ Member* m = new Member( ) ; /* the object "m" must be deleted otherwise a memory leak occurs */ delete m; }
    10. 10. THANK YOU
    11. 11. If this presentation helped you, please visit our page and like it. Thanks in advance. | |
    12. 12. Contact Us 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 Start up Village Eranakulam, Kerala, India. Email: