Select Page

Stack and Heap when working with pointers and functions

by 17.04.2023Pointers and functions in C

Using pointers within the functions allows you to pass data between the function and to modify them. Complex data can be also passed to function and returned from funtction in the form of a pointer to a structure.

Pointer can hold the address of a function, this provides us a mean to dynamically controll the execution flow fo a program.

It is important to understand the program stack. The program stack is used by a lot of modern block-structured blocks (such as C). The program stack supports the execution of functions.

When the function is invoked, the function’s stack frame is created and pushed onto the program stack. The function’s stack frame is popped off of the program stack when the function returns.

There are two situations where the pointers are useful when working with functions:

  1. When we pass a pointer to a function, we can modify data referenced by the pointer within the function and also we can pass efficiently blocks of information.
  2. When we declare a pointer to a function, we have a capability to control the execution flow of a program.

Program Stack and Heap

As I described different types of memory in the article “What are pointers?“, the local variables are also called automatic variables. The local variables are always allocated to a stack frame.

The program stack is an area of memory that is normally shared with the heap. It supports the execution of the functions. The program stack tends to occupy lower part of the region in the memory and the heap the upper part.

The program stack holds stack frames (also called activation records or activation frames). The stack frames hold the parameters and local variables of the functions. The heap manages dynamic memory.

When the function is called, its stak frame is pushed onto the stack. The stack grows upward. When the function terminates (returns), its stack frame is popped off the program stack. The memory used by stack is not cleared but it can be overridden by another stack frame that is pushed onto the stack.

Dynamically allocated memory comes from the heap. The heap tends to grow downward. The heap is fragmented as memory is allocated and deallocated.

Stack frame

Stack fraim contains more elements. For example return address is the location in the program where the function is returned after its completion. The storage for local data i the memory that was allocated for local variables. The storage for parameters is the memory that was allocated for the paramaters of the functions. Stack and base pointers are the pointers use by the runtime system to manage the stack.

The stack pointer usually points to the top of the stack. The stack base pointer (frame pointer) usually points to the address within the stack frame (such as return address). The stack base pointer is used to access the elements within the stack frame. These pointers are not C pointers but are used bz the runtime system to manage program stack. They become C pointers if the runtime system is implemented in C.

SOURCES:
[1] Understanding and Using C Pointers by Richard Reese (O’Reilly). Copyright 2013 Richard Reese, Ph.D. 978-1-449-34418-4