C Space Complexity

Intermediate
6 min read
Updated: Nov 2026
1 example

How Memory Grows with n

Space complexity measures extra memory an algorithm uses beyond its input: variables, buffers, and recursion stack frames. An iterative sum uses O(1) extra space; a recursive sum uses O(n) stack because each pending call keeps a frame. In-place algorithms (reverse, swap-based sorts) keep extra space at O(1).

Remember
int sum_iter(const int *a, int n);   /* O(1) extra space */
int sum_rec(const int *a, int n);    /* O(n) call stack  */

Cheat Sheet

Keep these facts handy while you work with Space Complexity in C.

TopicDetail
In placeO(1) extra
Copy of inputO(n)
Recursion depth dO(d) stack
AuxiliarySpace beyond the input

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Big O Notation in C.

Example 1 — Iterative sum versus recursive depth

Track how deep the recursion goes.

C
#include <stdio.h>

static int depth, max_depth;

static int rsum(const int *a, int n) {
    depth++;
    if (depth > max_depth) max_depth = depth;
    int r = (n == 0) ? 0 : a[n - 1] + rsum(a, n - 1);
    depth--;
    return r;
}

int main(void) {
    int a[] = {1, 2, 3, 4, 5};
    int total = 0;

    for (int i = 0; i < 5; i++) total += a[i];
    printf("iterative sum=%d (one accumulator)\n", total);

    int r = rsum(a, 5);
    printf("recursive sum=%d, max call depth=%d\n", r, max_depth);
    return 0;
}

How It Works

  1. The loop needs only total and i: constant extra space.
  2. The recursive version makes calls for n = 5, 4, 3, 2, 1, and 0.
  3. Six frames are alive at the deepest point, so stack space grows with n.

Edge Cases & Pitfalls

Check these before relying on Space Complexity in real programs.

stack

Ignoring the call stack

Recursion looks free but each call uses stack memory and can overflow.

copy

Needless copies

Copying an array to process it doubles memory use.

Key Takeaways

  • Count extra memory only.
  • Recursion costs stack.
  • In place is O(1).

One line: Space complexity measures extra memory an algorithm uses beyond its input: variables, buffers, and recursion stack frames.

Frequently Asked Questions

The amount of extra memory an algorithm needs as a function of input size.
Yes. Each active call uses a stack frame, so recursion depth d costs O(d) space.
Continue with "Big O Notation in C" next. Drop constants and lower-order terms to describe growth. The C Algorithm Basics hub lists every topic in order.

Next: Big O Notation in C

Drop constants and lower-order terms to describe growth.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful