← Back to list

Memory Management

Memory management refers to the process of either automatically or manually managing the allocation of memory to your variables in a…

Geoffrey Oliaro · 2020-10-20 18:04 · 0 claps · 3.4 min read
#c #malloc #memory-management #calloc
Open on Medium ↗
Wiki topics: BIZ · Business Strategy

Memory Management in C

Photo by Michael Dziedzic on Unsplash

Photo by Michael Dziedzic on Unsplash

Memory management refers to the process of either automatically or manually managing the allocation of memory to your variables in a program or how memory is accessed by a program. In this article I am mainly going to discuss dynamic memory allocation in C, with a slight overview of automatic and static memory allocation.

Static memory allocation occurs at compile time or rather, is handled by the compiler, if we consider that the compilation machine and the host machine might not be the same or might not even be on the same architecture. Memory cannot be increased during program execution and the lifetime of variables in static memory is the entire run of the program. Variable lifetime is contrasted with scope (where a variable can be used): “global” and “local” refer to scope, not lifetime, but scope often implies lifetime. In many languages, global variables are always static, but in some languages they are dynamic, while local variables are generally automatic, but may be static. Basically, the scope of static variables is the function or block in which they are declared(local scope or file scope) . The difference between local and static variables is that static variables preserve their values in between function invocations(storage duration), that is, the values are preserved even after the variables are out of their scope. Even so, no other code can ‘see’ them. In C, static memory is allocated by use of the ‘static’ keyword. In the case of global variables the static keyword is optional, however, when dealing with local variables it is mandatory

static int y = 76;

However to declare variables that will also be visible outside of the file in which it is declared(outside compilation module) you can use the ‘extern’ keyword

//this is done in some header file e.g. var.h
extern int x; //declaration
//this is done in some source file
#include “var.h”
int x; //definition

Automatic memory allocation occurs at execution time. Automatic or local variables are usually stored on the stack* (though the C standard doesn’t mandate that a stack is used). The programmer has limited control over the lifetime of this memory and its size is not flexible, for example, automatic variables in a function are only there until the function finishes.

void func() {
int i; // i only exists during `func`. This is i’s scope
}
printf(“%d”, i); // illegal as this is outside i’s scope

Note that scope does not necessarily mean function. Scopes can nest within a function, and the variable will be in-scope only within the block in which it was declared.

For example,

int* foo() {
int *a;
{
int b = 10; //b is in scope in this block
a=&b;
}
printf(“%p”, *a); //illegal since b is out of scope in this block
}

* Note also that where this memory is allocated is not specified. (On a sane system it will be on the stack, or registers for optimisation)

Dynamic memory management refers to manual memory management. This allows the programmer to control the exact size and lifetime of the program’s memory locations. The memory must be manually freed( since it’s persistent) to avoid memory leaks which may cause your application to crash, since at some point of time, the system will be unable to allocate more memory. Memory is allocated at runtime. Dynamic memory is also referred to as heap memory(has nothing to do with the heap data structure)

int func() {
int a = 5;
int* ptr;
ptr = (int*)malloc(sizeof(int));
ptr = &a;
printf(“%d %d”, a, *ptr”);
return 0;
}
*ptr = 10;
printf(“%d”, *ptr); //ptr is still accessible since free() has not been called to deallocate
free(mem);

There are 4 main library functions defined under <stdlib.h> for dynamic memory allocation.

Function

Use

malloc()

Allocates requested size of bytes and returns a pointer to the

first byte of allocated memory

calloc()

Allocates memory for array elements, initializes to zero and

then returns a pointer to memory

realloc()

Change the size of previously allocated memory

free()

Deallocate the previously allocated memory

malloc() stands for “memory allocation” and it reserves a block of memory of specified size and

returns a pointer of type void which can be casted into a pointer of any form. If the space is insufficient, allocation fails and returns NULL pointer.

Syntax: ptr = (cast-type*) malloc(byte-size)

Examples:

ptr = (char*) malloc(sizeof(char));
ptr2 = (int*)malloc(1024);

calloc stands for “contiguous allocation”.The difference between malloc() and calloc() is that malloc() allocates a single block of memory whereas calloc() allocates multiple blocks of memory each of same size and sets all bytes to zero.

Syntax: ptr = (cast-type*)calloc(n, element-size);

Will allocate contiguous space in memory for an array of n elements. For example:

ptr = (float*) calloc(25, sizeof(float));

Programmer explicitly uses free() to free memory

Syntax: free(ptr);

This statement frees the space allocated in the memory pointed to by ptr.

If the previously allocated memory is insufficient or more than required, one can change the

previously allocated memory size using realloc().

Syntax: ptr = realloc(ptr, newsize);

Here, ptr is reallocated with the size of newsize.


메타데이터
post_id
eea2b0eeadc6
slug
memory-management-eea2b0eeadc6
url
https://medium.com/@geoffreyoliaro/memory-management-eea2b0eeadc6
canonical_url
https://medium.com/@geoffreyoliaro/memory-management-eea2b0eeadc6
author_url
https://medium.com/@geoffreyoliaro
status
ok
fetched_at
2026-07-28 18:25:08