The document discusses memory allocation in programs using the stack and heap. The stack is used for function calls and allocates memory in LIFO order for local variables and function arguments. When a function exits, memory on the stack is reclaimed. The heap is used for dynamic memory allocation and grows as needed, requiring manual memory management through functions like new/delete. The stack is faster but limited in size, while the heap can allocate larger amounts but is slower.
PHP Frameworks: I want to break free (IPC Berlin 2024)
Stack and heap
1.
2. 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
3. INTRODUCTION
The memory a program uses is typically divided into four different areas:
• code area
• global area
• heap
• stack
4. 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.
5. 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.
6. 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.
7. #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;
}
8. 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.
9. 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.
10. 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;
}
12. 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
13. 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
Editor's Notes
The heap, where dynamically allocated variables are allocated from.The stack, where parameters and local variables are allocated from.