Difficulty: Beginner | Prerequisites: Basic C/C++ syntax, variables, and data types.
These three topics form the backbone of how C and C++ programs store and access data. Arrays let you group values under one name. Pointers let you refer to data by its location in memory rather than its name. Memory management ties the two together: when you need storage whose size you only learn at runtime, you allocate it yourself and clean it up when you are done. Nearly every non-trivial C/C++ program uses all three, and most of the bugs that haunt introductory students (segfaults, memory leaks, off-by-one errors) live here.
Arrays store multiple values in contiguous memory, accessed by index starting at 0. Pointers hold memory addresses and let you work with data indirectly. In C, you manage heap memory manually with malloc() and free(); forgetting to free causes leaks, and using memory after freeing causes crashes.
Array
A fixed-size, ordered collection of elements of the same type, stored in contiguous memory. Each element is accessed by a zero-based index.
In simple terms, think of it as a row of numbered boxes, all the same size, sitting side by side in memory.
Index (array index)
The integer position of an element within an array. The first element is at index 0, not 1.
In simple terms, this is the box number you use to pick out one item from the row.
Pointer
A variable whose value is the memory address of another variable. Declared with the * symbol in C/C++.
In simple terms, a pointer is like a sticky note that says "the data you want is over there, at this address."
Dereference (operator *)
Following a pointer to read or write the value stored at the address it holds.
In simple terms, if the pointer is the sticky note, dereferencing is walking to the address written on it and looking at what is there.
Address-of operator (&)
Returns the memory address of a variable, which can then be stored in a pointer.
In simple terms, &x asks "where in memory does x live?" and hands you back that location.
malloc()
A C standard library function that allocates a specified number of bytes on the heap and returns a pointer to the start of that block.
In simple terms, malloc asks the operating system for a chunk of memory and gives you back the address of the first byte.
free()
A C standard library function that releases heap memory previously allocated by malloc(). After calling free(), that memory is no longer yours to use.
In simple terms, free hands the memory back to the system so it can be reused.
Heap (dynamic memory)
A region of memory used for allocations that persist beyond a single function call and whose size may not be known at compile time.
In simple terms, the heap is a big pool of memory you can borrow from at runtime, as long as you return what you borrow.
Stack (automatic memory)
Memory automatically allocated when a function is called and released when it returns. Local variables and arrays with fixed sizes live here.
In simple terms, the stack is scratch space that comes and goes with each function call.
An array is declared with a type, a name, and a size in square brackets:
int numbers[5] = {1, 2, 3, 4, 5};
string fruits[3] = {"apple", "banana", "orange"};
Elements are accessed by index, starting at 0:
numbers[0] is 1, numbers[2] is 3.
fruits[1] is "banana".
The size is fixed at compile time for stack-allocated arrays. If you need a size determined at runtime, you move to dynamic allocation (see Memory Management below).
Arrays can represent one-dimensional lists or, with nested brackets, two-dimensional grids and matrices.
Declare a pointer with the * symbol:
int *agePtr; declares a pointer to an int.
Assign an address using &:
int age = 25;
int *agePtr = &age;
Dereference with * to get or set the value at the address:
cout << *agePtr; outputs 25.
*agePtr = 30; changes age to 30 through the pointer.
Pointers are essential for three things in C/C++:
Passing large data to functions without copying it.
Dynamic memory allocation (requesting memory at runtime).
Accessing hardware resources and low-level system interfaces.
In C, heap memory is allocated with malloc() and released with free():
int numbers = (int)malloc(5 * sizeof(int));
This allocates space for 5 integers and returns a pointer to the first one.
You must cast the void* returned by malloc to the appropriate pointer type (e.g. (int*)).
After use, always free the memory:
free(numbers);
Some languages (Java, Python) manage memory automatically via garbage collection. C and C++ do not, at least not by default, which is why this topic exists.
Array memory layout (conceptual)
For int numbers[5] = {1, 2, 3, 4, 5};, assuming each int is 4 bytes and the array starts at address 0x100:
Index | Value | Address |
|---|---|---|
0 | 1 | 0x100 |
1 | 2 | 0x104 |
2 | 3 | 0x108 |
3 | 4 | 0x10C |
4 | 5 | 0x110 |
malloc size formula
pointer = (type*)malloc(count * sizeof(type));
Where count is how many elements you need and sizeof(type) gives the byte size of each one.
Arrays underpin everything from pixel buffers in image processing to the rows of a database result set held in application memory. Pointers and manual memory management are why C remains the language of operating system kernels and embedded firmware: you control exactly what memory you use and when you release it, which matters when resources are measured in kilobytes rather than gigabytes.
Students often think array indices start at 1. They start at 0 in C/C++. Accessing numbers[5] on a 5-element array is out of bounds, not the last element.
Students often confuse the declaration (this is a pointer) with the dereference (go to the address). They are the same symbol used in two different contexts.
Students often assume malloc initialises memory to zero. It does not; the bytes contain whatever was there before. Use calloc() if you need zero-initialised memory.
Students often forget to call free() and assume the program will clean up. The OS reclaims memory when the program exits, but during execution, leaked memory accumulates and can exhaust available resources.
⚠️ Off-by-one errors on array access are a perennial exam and interview question. Know that valid indices for an array of size n run from 0 to n-1.
⚠️ Expect questions asking you to trace pointer values: given int x = 10; int p = &x;, what is p? What happens after *p = 20;?
⚠️ Memory leak questions: "What is wrong with this code?" where malloc is called but free is missing.
⚠️ Know the difference between stack and heap allocation and when each is appropriate.
True or false: numbers[5] is a valid access for int numbers[5].
Fill in the blank: The & operator returns the _______ of a variable.
True or false: malloc() returns memory initialised to zero.
Fill in the blank: To release dynamically allocated memory in C, you call _______.
True or false: A pointer declared as int *p; stores an integer value directly.
Answers: 1. False (valid indices are 0 to 4). 2. Memory address. 3. False. 4. free(). 5. False (it stores an address).
Q: Given int arr[4] = {10, 20, 30, 40};, what value does arr[2] hold?
A: 30, because indexing starts at 0.
Q: What does the following code print?
int x = 7;
int *p = &x;
*p = 14;
printf("%d", x);
A: 14. The pointer p holds the address of x. Dereferencing and assigning through p changes the value of x.
Q: What is a memory leak, and how does it occur in C?
A: A memory leak occurs when dynamically allocated memory (via malloc) is never released with free(). The program loses the ability to use or reclaim that memory for the duration of its execution.
Q: Write a one-line statement that dynamically allocates an array of 10 doubles in C.
A: double arr = (double)malloc(10 * sizeof(double));
Q: What is the difference between stack and heap memory?
A: Stack memory is allocated and freed automatically with function calls and returns. Heap memory is allocated manually (e.g. with malloc) and persists until explicitly freed. Stack is faster but limited in size; the heap is larger but requires the programmer to manage it.
This material connects directly to data structures (linked lists, trees, graphs all rely on pointers and dynamic allocation) and to operating systems concepts (virtual memory, paging, and how the OS manages the heap). Understanding pointer arithmetic also prepares you for string manipulation in C, where strings are just arrays of char terminated by '\0'.
array, pointer, memory management, malloc, free, calloc, heap, stack, dynamic allocation, static allocation, dereference, address-of operator, segfault, segmentation fault, memory leak, dangling pointer, null pointer, C arrays, C pointers, CSE 101, UCF Computer Science I, zero-based indexing, sizeof