• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Stack and heap
 

 

Statistics

Views

Total Views
602
Views on SlideShare
602
Embed Views
0

Actions

Likes
0
Downloads
1
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • The heap, where dynamically allocated variables are allocated from.The stack, where parameters and local variables are allocated from.

Stack and heap Stack and heap Presentation Transcript

  • FUNCTIONS WITH STACK AND HEAP Anjali G anjalig2009@gmail.com dwww.facebook.com/Anjali Geetha twitter.com/Anjali Geetha in.linkedin.com/in/Anjali G 9497879952
  • INTRODUCTION The memory a program uses is typically divided into four different areas: • code area • global area • heap • stack
  • 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.
  • 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.
  • 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.
  • #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; }
  • 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.
  • 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.
  • 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; }
  • THANK YOU
  • If this presentation helped you, please visit our page facebook.com/baabtra and like it. Thanks in advance. www.baabtra.com | www.massbaab.com |www.baabte.com
  • 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: info@baabtra.com