Skip to content
Home » All Posts » A Deeper Look at Computer Memory

A Deeper Look at Computer Memory

computer memory dissected

What is up with Computer Memory?

There are several different types of computer memories in the context of computing and programming. The type of computer memory we most often refer to is RAM (Random Access Memory). This type of memory has very fast reading and writing rate, as opposed to physical hard disk. This high speed is absolutely vital as it ensures that computer applications can run smoothly.

Without enough RAM, the operating system will reserve a portion of space on your hard disk (also known as the swap memory space) to use as memory at much slower speed. This causes applications to run significantly slower. Also, most application crashes are associated with memory violations. We use memory address to represent a particular location of a memory, which is normally expressed in hexadecimal values.

In terms of C programming, you need to know these 3 main sections of memory: Stack, Heap and Read Only

computer memory

The Stack Memory

  • Stack memory is a simple data structure having continuous blocks of memory
  • Works in Last In First Out (LIFO) fashion.
  • Allocation happens automatically in the function call stack
  • A function’s local variables are stored in stack memory
  • Deallocation happens automatically when the function returns
  • High speed access
  • Normally very small and limited in size, 1 ~ 8 MB is typical
  • Stack overflow happens when all of the stack memory is exhausted.

Consider this C code:

which corresponds to the following stack memory:

The Heap Memory

  • It is a pile of computer memory space available to programmers to allocate and deallocate
  • Allocated by programmers manually during program run time, normally using malloc() and pointers
  • Deallocated by programmers manually during program run time, normally using free()
  • Requires more care to manage or memory leak can happen to the program
  • Slower than stack memory
  • How much computer memory is available depending on your RAM

Consider this C code:

which corresponds to the following heap memory:

The Read Only Memory

  • The data here is set when the memory is allocated automatically
  • Obviously the data here can only be read by your program
  • Any attempt to modify data in read-only memory will result in segmentation fault (aka. Program crashed).
  • Programmer does not need to manually manage this memory space

Consider this C code:

which corresponds to the following read only memory:

Static Memory Allocation

Static memory allocation happens when you declares a variable inside a function, defines or calls a function. This allocation process allocates stack memory automatically so you do not need to write any code to manage it. The stack memory is automatically released at the end of the life time (end of function, or application quits). Despite being automatic, it is still important to pay attention to it so we can avoid stack overflow.

In the example below, the variable a inside the function staticAllocExample() is statically allocated on the stack memory and is destroyed when the function ends.

void staticAllocExample(void)
{
	int a = 50;	// static memory allocation (stack memory)
	printf("a = %d\n", a);
}
int main(void)
{
	staticAllocExample();
	return 0;
}

So, what is stack overflow? Stack overflow refers to a situation where the stack memory becomes full and exceeds its predefined size. This is normally caused by excessive function calls or infinite recursive calls.

In the example below, every call to the function stackOverflowExample() will allocate 1024 bytes of stack memory for mychar variable, and it will call itself again (recursive call). Notice that there is no stop condition for this recursive call, so this setup will eventually exhaust all the stack memory and cause unexpected problems or simply applicatiion abort.

void stackOverflowExample(void)
{
	char mychar[1024] = {0};
	stackOverflowExample();
}
int main(void)
{
	stackOverflowExample();
	return 0;
}

Dynamic Memory Allocation

Dynamic Memory Allocation happens when you explicitly requests heap memory from the operating system using standard C routines like malloc() or calloc(). Memory allocated dynamically will live until the end of the program or until you explicitly deallocate it via the free() call. This is what makes C programming infamous because with dynamic memory allocation, you are fully responsible for managing the memory yourself and avoiding “memory leak”.

In the example below, the function dynamicAllocExample() dynamically allocates a piece of heap memory the size of a int and gives it a value of 50. The reference to the allocated heap memory is returned by the function as an integer pointer (int *). On the main function, we are able to access this pointer to the heap memory because it is still valid (even after dynamicAllocExample() returns). The value of this memory is printed and then destroyed by the free() call at the end of the main function.

int * dynamicAllocExample(void)
{
	int * b = NULL;
	b = (int*) malloc(sizeof(int));	//dynamic memory allocation (heap memory)
	if (!b)
	{
		printf("failed to allocate memory from heap!\n");
		return NULL;
	}
	*b = 50;
	return b;
}
int main(void)
{
	int * b = NULL;
	b = dynamicAllocExample();
	if(b)
	{
		printf("b = %d\n", *b);
		free(b);
		b = NULL;
	}
	return 0;
}

Memory Deallocation and Memory Leak

In the previous example, we use malloc() to allocate a piece of heap memory, which is valid until the end of the program or until we explicitly destroy it with free() function call. This memory allocation is taken directly from your Operating System (OS) as a virtualized memory mapped directly to your physical RAM. This means that there is a limit on the total amount of memory that can be allocated by applications.

When one application has allocated most of the available memory, it means other applications have less memory to allocate. When there is no RAM memory available for allocation, memory swap will happen, which means some of the physical disks are used as RAM to meet the demand. This also means that your whole system will slow down significantly.

Memory Leak refers to a scenario where an application keeps allocating heap memory without releasing it (by a call to free()when not in use. This is mostly due to a programming mistake and may not be easy to spot. It seems harmless at first because the system may still have plenty of memory available. However, if your application is a “continuously running” application, the problem will start to arise after running for a long time. Thankfully, a tool like “valgrind” can be used to examine your application for potential memory leaks.

So, please remember to release your heap memory back to the OS when you are done with it.

Other Related Reads

Join the conversation

Your email address will not be published. Required fields are marked *