Stack and Heap Memory

What is memory ?

Memory of computer is like a brain of human which can store data and instructions required during the processing of data and output results. Computer Memory is a physical device capable of storing information temporary and permanent. The performance of computer mainly based on memory and CPU. There are three type of memory: cache, primary memory, and secondary memory.

Cache

Cache is the smallest size of a volatile memory. It is the high-speed data access (I/O) and is directly communicated with central processing unit (CPU). The disadvantages are limited storage and expensive.

Primary memory

Primary memory is main memory, access data first and directly. Random accessing memory (RAM) is one of the type primary memory which is a volatile memory. RAM can only maintain the data when the devices is powered. Another primary memory is read only memory (ROM) which is only read by processors. The new data cannot be written in ROM.

Secondary memory

Secondary memory is non-volatile memory, does not directly communicate with processors. The example of this type is hard disk drive (HDD). It can have a large storage, but it is quite slow for I/O data access. You are allowed to rewrite data to HDD. The data maintained in HDD can also be replaced.

What is Stack and Heap ?

In programming language, the memory allocation (MA) is very important, especially in mid- and high-level programming such as C and C++. Performance (speed) of program depends on how it accesses data (I/O) and communicates the processors. Therefore, managing MA is very necessary for professional coding. Allocating memory includes stack and heap memory.

Stack

Stack is a static memory.

  • Variable scope: Local variable
  • Fast data access
  • Memory leak can be occurred
  • Limited size of variable
  • Fixed size of variable


Here is an example of creating variables on the stack memory.

#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 %.3f\n", multiplyByTwo(salary));

  return 0;
}

Heap

Heap is a dynamics memory.

  • Variable scope: Global variable
  • Slow data access
  • No limit on memory size
  • Variable can be resized e.g. use realloc() in C programming
  • User-adjusted memory


Here is an example of creating variables on the heap memory.

You can see that the variable is assigned as global variable (heap) by using memory allocation function malloc() and calloc(), and releasing the variable using free memory function free() in order to clear memory free space. They deal with pointers which denoted by * asterisk symbol.

#include <stdio.h>
#include <stdlib.h>

double *multiplyByTwo (double *input) {
  double *twice = malloc(sizeof(double));
  *twice = *input * 2.0;
  return twice;
}

int main (int argc, char *argv[])
{
  int *age = malloc(sizeof(int));
  *age = 30;
  double *salary = malloc(sizeof(double));
  *salary = 12345.67;
  double *myList = malloc(3 * sizeof(double));
  myList[0] = 1.2;
  myList[1] = 2.3;
  myList[2] = 3.4;

  double *twiceSalary = multiplyByTwo(salary);

  printf("double your salary is %.3f\n", *twiceSalary);

  free(age);
  free(salary);
  free(myList);
  free(twiceSalary);

  return 0;
}

When to deal with stack and memory ?

When you deal with a small amount of variable or small size of memory (fast in, fast out), stack memory is required. On the other hand, heap memory should be considerably used when you create data on the fly (when you need).

Where can I find stack and heap memory control ?

The impact of stack and heap memory allocation on program performance, e.g. computational chemistry program package, is NWChem. Memory allocation by NWChem calculation is freely adjustable, including stack, heap, and global. Stack and Heap directives in NWChem is a node-private memory, while Global directive is controlling memory of a shared-memory crossing over node and processors.

Rangsiman Ketkaew