From the free course Systems Programming in C and Unix Environments on Lykke
Memory Management, Objects, and Pointers
Key concepts: Pointer Arithmetic and Offsets · Null-terminated Strings · Pass-by-reference · Dereferencing and Address-of Operators · Void Pointers
Deep dive into C's memory model, pointer arithmetic, and the handling of arrays and strings.
Memory Management, Objects, and Pointers
In systems programming, memory is not an abstract container but a physical and logical resource that must be mapped, managed, and manipulated with mathematical precision. In the C programming language, we treat memory as a massive, contiguous array of bytes, where each byte is identified by a unique numerical address. To master C is to master the relationship between these addresses and the data they represent.
The C Memory Model: Objects and Lifetimes
Before discussing pointers, we must define the object. In the context of C, an object is a region of data storage in the execution environment, the contents of which can represent values. Every object has a size, a type, and a lifetime (or storage duration). The lifetime determines when the memory is allocated and when it is guaranteed to be valid.
The Three Pillars of Storage Duration
C categorizes memory into three primary regions, each serving a distinct architectural purpose:
| Storage Class | Allocation Timing | Deallocation Timing | Scope/Visibility | Typical Location |
|---|---|---|---|---|
| Static | Program Startup | Program Termination | Global or File-level | Data/BSS Segment |
| Stack | Function Entry | Function Exit (Return) | Local to block | Stack Segment |
| Heap | Manual (malloc) |
Manual (free) |
Pointer-dependent | Heap Segment |
- Static Objects: These exist for the entire duration of the program. Global variables and variables declared with the
statickeyword fall into this category. They are useful for maintaining state across function calls but can lead to issues in multi-threaded environments. - Stack (Automatic) Objects: These are the most common. When you declare
int x;inside a function, it is "pushed" onto the stack. When the function returns, the stack pointer moves back, and that memory is effectively reclaimed. - Heap (Dynamic) Objects: These are managed manually by the programmer. Using
malloc,calloc, orrealloc, a programmer requests a specific number of bytes. This memory remains allocated until it is explicitly released viafree.
The Golden Rule of Lifetimes: Accessing a stack object after its function has returned, or a heap object after it has been freed, results in Undefined Behavior (UB). The pointer becomes "dangling," pointing to memory that may have been repurposed by the OS or the runtime.
Dereferencing and Address-of Operators
To bridge the gap between a variable name and its location in memory, C provides two fundamental operators: the Address-of operator (&) and the Dereference operator (*).
The Address-of Operator (&)
The & operator is a unary operator that returns the memory address of its operand. If x is an integer, &x is a pointer to an integer (int *).
The Dereference Operator (*)
The * operator is the inverse. When applied to a pointer, it accesses the value stored at the address the pointer holds. This is often called "indirection."
int main() {
int val = 42;
int *ptr = &val; // ptr now holds the address of val
printf("Value: %d\n", val); // Outputs 42
printf("Address: %p\n", ptr); // Outputs the hex address (e.g., 0x7ffee...)
printf("Deref: %d\n", *ptr); // Outputs 42
*ptr = 100; // Changing the value via the pointer
printf("New Value: %d\n", val); // val is now 100
return 0;
}
Pointer Arithmetic and Offsets
One of the most powerful—and dangerous—features of C is Pointer Arithmetic. Unlike standard integer arithmetic, pointer arithmetic is automatically scaled by the size of the data type the pointer references.
The Scaling Mechanism
When you add an integer n to a pointer p of type T*, the resulting address is not p + n. Instead, it is:
$$\text{New Address} = \text{Base Address} + (n \times \text{sizeof}(T))$$
This ensures that incrementing a pointer (ptr++) always moves it to the next element of that type in memory, regardless of whether the type is a 1-byte char, a 4-byte int, or a 128-byte struct.
| Operation | Expression | Resulting Address Change |
|---|---|---|
| Increment | ptr++ |
+ sizeof(type) |
| Decrement | ptr-- |
- sizeof(type) |
| Addition | ptr + i |
+ (i * sizeof(type)) |
| Subtraction | ptr1 - ptr2 |
(Addr1 - Addr2) / sizeof(type) |
Array-Pointer Duality
In C, the name of an array acts as a constant pointer to its first element. The expression a[i] is actually "syntactic sugar" for *(a + i).
Proof of Duality:
If a[i] is equivalent to *(a + i), then by the commutative property of addition, *(a + i) is equivalent to *(i + a). This implies that i[a] is also valid C code.
int arr[5] = {10, 20, 30, 40, 50};
printf("%d\n", arr[2]); // Outputs 30
printf("%d\n", 2[arr]); // Also outputs 30!
Note: While i[a] is valid, it is considered extremely poor practice and is used only to demonstrate the underlying mechanics of pointer arithmetic.
Null-terminated Strings
C does not have a native "string" type in the way modern languages like Python or Java do. Instead, a string is defined by convention: an array of char elements ending with a null terminator (\0, which has the ASCII value 0).
The Importance of \0
Functions like printf, strlen, and strcpy do not know the size of the character array they are processing. They simply start at the provided pointer and continue reading bytes until they encounter a 0.
Common Pitfall: The Off-by-One Buffer
When allocating memory for a string, you must always account for the null terminator. To store the word "C-Style" (7 characters), you need an array of size 8.
char *str = malloc(8 * sizeof(char));
strcpy(str, "C-Style"); // Copies 'C','-','S','t','y','l','e' AND '\0'
If the null terminator is missing, string functions will continue reading past the end of the array into adjacent memory, leading to "garbage" output or segmentation faults. This is the root cause of many security vulnerabilities, such as buffer overflow attacks.
Pass-by-Reference (via Pointers)
Strictly speaking, C is a pass-by-value language. When you pass an argument to a function, a copy of that value is made on the stack. However, we can simulate pass-by-reference by passing the address of a variable (a pointer).
Motivation: The Swap Function
Consider a function intended to swap two integers. If we pass them by value, the function only swaps the copies, leaving the originals unchanged.
// WRONG: Pass-by-value
void swap_bad(int a, int b) {
int temp = a;
a = b;
b = temp;
}
// CORRECT: Simulating Pass-by-reference
void swap_good(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
Passing Arrays
When an array is passed to a function, it "decays" into a pointer to its first element. This is why you must almost always pass the size of the array as a separate argument, as the function no longer knows the original array's bounds.
| Method | Syntax | Effect |
|---|---|---|
| By Value | void func(int x) |
Local copy; original is safe. |
| By Reference | void func(int *x) |
Direct access; original can be modified. |
| Array | void func(int arr[]) |
Decays to int *; original can be modified. |
Void Pointers and Generic Programming
The void * is a special pointer type known as a generic pointer. It can hold the address of any data type without an explicit cast. However, there is a catch: you cannot dereference a void * or perform pointer arithmetic on it directly, because the compiler does not know the size of the underlying type.
Usage in Systems Programming
void * is essential for generic functions. The standard library function malloc returns a void * because it doesn't know what kind of data you intend to store in the allocated memory.
void print_bytes(void *ptr, int num_bytes) {
// We must cast to char* to perform 1-byte pointer arithmetic
unsigned char *p = (unsigned char *)ptr;
for (int i = 0; i < num_bytes; i++) {
printf("%02x ", p[i]);
}
printf("\n");
}
In the example above, casting to unsigned char * allows us to iterate through the memory byte-by-byte, regardless of whether the original data was an int, a double, or a custom struct.
Advanced Concept: Pointer Offsets and Struct Alignment
When dealing with complex data structures (structs), the compiler often inserts "padding" bytes between members to ensure that each member starts at a memory address that is a multiple of its size (alignment).
Alignment Theorem: For efficient CPU access, a data object of size $N$ bytes should typically be stored at an address $A$ such that $A \pmod N = 0$.
Worked Example: Struct Padding
struct Data {
char a; // 1 byte
// 3 bytes of padding inserted here
int b; // 4 bytes
char c; // 1 byte
// 3 bytes of padding inserted here
};
Even though the data only totals 6 bytes, sizeof(struct Data) will likely return 12. Understanding pointer offsets is critical when using void * to manually navigate these structures or when serializing data for network transmission.
Common Pitfalls and Memory Safety
The power of pointers comes with significant responsibility. Senior engineers focus heavily on avoiding the following "Big Three" memory errors:
- Memory Leaks: Allocating memory on the heap (
malloc) and losing the pointer to it before callingfree. Over time, the program consumes all available RAM. - Segmentation Faults (Segfaults): Attempting to dereference a
NULLpointer or an address that the program does not have permission to access. - Buffer Overflows: Writing past the end of an allocated block. This can overwrite the "Return Address" on the stack, allowing attackers to hijack the execution flow of the program.
The Null Pointer
A pointer assigned the value 0 (or the macro NULL) is guaranteed to point to "nothing." It is a best practice to initialize pointers to NULL and to check if a pointer is NULL before dereferencing it.
int *p = malloc(sizeof(int));
if (p == NULL) {
fprintf(stderr, "Memory allocation failed\n");
exit(1);
}
// Safe to use p
Summary of Pointer Operations
To synthesize these concepts, consider the following table which summarizes how different operators interact with memory:
| Operator | Name | Purpose | Mathematical View |
|---|---|---|---|
& |
Address-of | Get location of a variable | $Var \rightarrow Addr$ |
* |
Dereference | Get value at a location | $Addr \rightarrow Value$ |
+ / - |
Arithmetic | Move through contiguous memory | $Addr \pm (n \times Size)$ |
[] |
Subscript | Access element at index | $*(Addr + (i \times Size))$ |
-> |
Arrow | Access struct member via pointer | (*ptr).member |
Source Materials
Study Systems Programming in C and Unix Environments on Lykke
This course page is free to open: signed out, you can read its first two sections, and signing in, on any plan, opens the rest. The free Lykke Chrome extension syncs your own Canvas courses, so ChatGPT, Claude and Muse can answer from your classes and deadlines. It only reads Canvas.
Add the free extensionSign in to open every sectionAll sections of Systems Programming in C and Unix Environments · Browse more course pages