Why dynamic memory is needed - Performance Analysis
We want to understand how using dynamic memory affects the time it takes for a program to run.
Specifically, we ask: How does allocating memory while the program runs impact performance?
Analyze the time complexity of the following code snippet.
#include <stdlib.h>
int* createArray(int n) {
int* arr = (int*)malloc(n * sizeof(int));
for (int i = 0; i < n; i++) {
arr[i] = i * 2;
}
return arr;
}
This code allocates memory for an array of size n during runtime and fills it with values.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for loop that fills the array.
- How many times: It runs n times, once for each element.
As n grows, the number of steps to fill the array grows directly with n.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 steps to fill the array |
| 100 | About 100 steps to fill the array |
| 1000 | About 1000 steps to fill the array |
Pattern observation: The work grows evenly as the input size grows.
Time Complexity: O(n)
This means the time to fill the array grows in direct proportion to the number of elements.
[X] Wrong: "Allocating memory dynamically is instant and does not affect performance."
[OK] Correct: Allocating memory takes time, and filling that memory with values also takes time proportional to the size.
Understanding how dynamic memory allocation affects time helps you write efficient programs and explain your choices clearly.
What if we changed the loop to fill only half the array? How would the time complexity change?
Practice
Solution
Step 1: Understand static vs dynamic memory
Static memory is fixed at compile time, so it cannot change size during execution.Step 2: Identify the need for flexible memory
Dynamic memory allows programs to request memory as needed during runtime, useful when data size is unknown beforehand.Final Answer:
To allocate memory during program execution when size is unknown -> Option AQuick Check:
Dynamic memory = flexible size allocation [OK]
- Confusing dynamic memory with faster execution
- Thinking dynamic memory avoids pointers
- Believing dynamic memory stores only constants
Solution
Step 1: Recall malloc syntax
malloc requires the number of bytes to allocate, so multiply count by size of type.Step 2: Check each option
int *ptr = malloc(10 * sizeof(int)); correctly multiplies 10 by sizeof(int) and assigns to int pointer.Final Answer:
int *ptr = malloc(10 * sizeof(int)); -> Option BQuick Check:
malloc size = count * sizeof(type) [OK]
- Not multiplying by sizeof(int)
- Assigning malloc result to non-pointer
- Allocating too little memory
int *arr = malloc(3 * sizeof(int));
arr[0] = 5;
arr[1] = 10;
arr[2] = 15;
printf("%d", arr[1]);
free(arr);Solution
Step 1: Understand array assignment
arr[1] is assigned the value 10 in the code.Step 2: Check printf output
printf prints arr[1], so output is 10.Final Answer:
10 -> Option DQuick Check:
arr[1] = 10, so output = 10 [OK]
- Confusing index positions
- Forgetting to multiply by sizeof(int) in malloc
- Assuming free causes error here
int *ptr;
ptr = malloc(5);
ptr[0] = 1;
printf("%d", ptr[0]);
free(ptr);Solution
Step 1: Check malloc size argument
malloc(5) allocates 5 bytes, but int usually needs 4 bytes each, so 5 bytes is too small for 5 ints.Step 2: Correct malloc size
It should be malloc(5 * sizeof(int)) to allocate enough memory.Final Answer:
malloc size is too small, should be 5 * sizeof(int) -> Option CQuick Check:
malloc size = count * sizeof(type) [OK]
- Ignoring correct malloc size
- Thinking free must come before malloc
- Assuming ptr is uninitialized error
Solution
Step 1: Understand unknown input size
Since the number of integers is unknown, fixed arrays or single malloc calls are insufficient.Step 2: Use realloc for flexible resizing
Start with malloc, then use realloc to increase memory as more integers are entered.Final Answer:
Use malloc initially, then realloc to expand as needed -> Option AQuick Check:
Unknown size needs realloc for resizing [OK]
- Using fixed arrays for unknown input
- Not resizing memory dynamically
- Avoiding pointers for dynamic data
Ready to prove it?
Pass the test and this pattern moves unknown
