-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathc-stack.html
More file actions
315 lines (277 loc) · 17 KB
/
Copy pathc-stack.html
File metadata and controls
315 lines (277 loc) · 17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Stack in C: Static and Dynamic Array Implementation | EasyStack</title>
<meta name="description" content="Build a stack in C using static arrays and dynamically allocated memory. Learn manual memory management, pointer-based stack design, and memory safety." />
<meta name="keywords" content="c stack, stack in c, static array stack, dynamic array stack, linked list stack c, malloc free stack, memory management c, stack overflow underflow" />
<meta name="author" content="Arun Neupane" />
<meta name="robots" content="index, follow" />
<link rel="canonical" href="https://easystack.netlify.app/c-stack" />
<meta name="theme-color" content="#F2F2F7">
<meta name="google-adsense-account" content="ca-pub-1708134460872611">
<meta property="og:title" content="Stack in C: Static and Dynamic Array Implementation" />
<meta property="og:description" content="Build a stack in C using static arrays and dynamically allocated memory. Learn manual memory management, pointer-based stack design, and memory safety." />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://easystack.netlify.app/c-stack" />
<meta property="og:image" content="https://easystack.netlify.app/images/social-card.png" />
<meta property="og:image:width" content="1200" />
<meta property="og:image:height" content="630" />
<meta property="og:site_name" content="EasyStack" />
<meta property="og:locale" content="en_US" />
<meta name="twitter:card" content="summary" />
<meta name="twitter:title" content="Stack in C: Static and Dynamic Array Implementation" />
<meta name="twitter:description" content="Build a stack in C using static arrays and dynamically allocated memory. Learn manual memory management, pointer-based stack design, and memory safety." />
<link rel="icon" href="./images/stack-fav.png" type="image/x-icon">
<link rel="apple-touch-icon" href="./images/stack-fav.png">
<link rel="stylesheet" href="./hig.css">
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "Article",
"headline": "Stack in C: Static and Dynamic Array Implementation",
"description": "Build a stack in C using static arrays and dynamically allocated memory. Learn manual memory management, pointer-based stack design, and memory safety.",
"author": {"@type": "Person", "name": "Arun Neupane", "url": "https://arunneupane.netlify.app/"},
"publisher": {"@type": "Organization", "name": "EasyStack", "url": "https://easystack.netlify.app", "logo": {"@type": "ImageObject", "url": "https://easystack.netlify.app/images/stack-fav.png", "width": 512, "height": 512}},
"datePublished": "2026-09-01",
"dateModified": "2026-09-01",
"mainEntityOfPage": "https://easystack.netlify.app/c-stack"
}
</script>
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "FAQPage",
"mainEntity": [
{"@type": "Question", "name": "Should I use a fixed array or dynamic memory for a C stack?", "acceptedAnswer": {"@type": "Answer", "text": "Use a fixed array when the maximum stack size is known in advance and stays small, because it is simple, fast, and needs no allocation. Use dynamic memory with realloc when the stack must grow unpredictably, because that lets it scale without wasting memory in advance."}},
{"@type": "Question", "name": "What is stack overflow and underflow in a C stack?", "acceptedAnswer": {"@type": "Answer", "text": "Stack overflow happens when you push onto a full stack: the top index reaches the capacity. Stack underflow happens when you pop from an empty stack: the top index is below zero. Both must be checked explicitly in C, because C does not throw exceptions or bounds-check arrays."}},
{"@type": "Question", "name": "How do I avoid memory leaks in a dynamic C stack?", "acceptedAnswer": {"@type": "Answer", "text": "Free every block you malloc. For a dynamic array stack, call free() on the array in a destroy function. For a linked list stack, pop nodes and free each one until the list is empty. Use a tool like valgrind or AddressSanitizer to verify there are no leaks or invalid frees."}}
]
}
</script>
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "BreadcrumbList",
"itemListElement": [
{"@type": "ListItem", "position": 1, "name": "Home", "item": "https://easystack.netlify.app/"},
{"@type": "ListItem", "position": 2, "name": "Guides", "item": "https://easystack.netlify.app/guides"},
{"@type": "ListItem", "position": 3, "name": "Stack in C", "item": "https://easystack.netlify.app/c-stack"}
]
}
</script>
</head>
<body>
<a class="skip-link" href="#main">Skip to content</a>
<header class="topbar">
<div class="shell topbar__inner">
<a class="brand" href="./"><span class="brand__mark brand__mark--logo"><img src="./images/stack.gif" alt="" width="30" height="30"></span> EasyStack</a>
<nav aria-label="Primary"><ul class="nav-links desktop-links">
<li><a href="./">Visualizer</a></li><li><a href="./guides">Guides</a></li><li><a href="./stack-visualizer">Tool</a></li><li><a href="./stack-frame-visualizer">Call Stack</a></li><li><a href="./stack-interview">Interview</a></li>
</ul></nav>
<button class="theme-toggle" id="themeToggle" type="button" title="Toggle dark mode"></button>
<button class="nav-toggle" id="navToggle" type="button" aria-label="Menu" aria-expanded="false" aria-controls="navLinks"><span></span><span></span><span></span></button>
<ul class="nav-links nav-mobile" id="navLinks">
<li><a href="./">Visualizer</a></li><li><a href="./guides">All Guides</a></li><li><a href="./stack-operations">Stack Operations</a></li><li><a href="./stack-visualizer">Advanced Tool</a></li><li><a href="./stack-frame-visualizer">Call Stack Visualizer</a></li><li><a href="./stack-complexity">Complexity</a></li><li><a href="./stack-interview">Interview Problems</a></li><li><a href="./about">About</a></li>
</ul>
</div>
</header>
<main id="main">
<section class="hero">
<div class="shell">
<h1>C</h1>
<p>Build a stack in C from scratch with static arrays and dynamic memory, and learn the manual memory management that every C program requires.</p>
</div>
</section>
<section class="guide-content">
<div class="shell">
<h2>Static Array Stack</h2>
<p>The simplest C stack uses a fixed-size array plus a top index. The <code>struct</code> holds the array and the current top position, and push and pop manipulate that index.</p>
<pre><code>#include <stdio.h>
#include <stdbool.h>
#define MAX 100
typedef struct {
int data[MAX];
int top;
} Stack;
void init(Stack *s) { s->top = -1; }
bool push(Stack *s, int value) {
if (s->top >= MAX - 1) return false; // overflow
s->data[++s->top] = value;
return true;
}
bool pop(Stack *s, int *value) {
if (s->top < 0) return false; // underflow
*value = s->data[s->top--];
return true;
}
bool peek(const Stack *s, int *value) {
if (s->top < 0) return false;
*value = s->data[s->top];
return true;
}
bool empty(const Stack *s) { return s->top < 0; }</code></pre>
<p>The top starts at -1, meaning an empty stack. Pushing increments it, popping decrements it. C does no bounds checking, so you must verify overflow and underflow yourself before every push and pop.</p>
<h2>Dynamic Array Stack</h2>
<p>When you do not know the maximum size in advance, allocate the array on the heap and grow it with <code>realloc</code>. The struct stores a pointer, the current top, and the current capacity.</p>
<pre><code>#include <stdlib.h>
typedef struct {
int *data;
int top;
int capacity;
} DynStack;
void init_dyn(DynStack *s, int cap) {
s->data = malloc(cap * sizeof(int));
s->top = -1;
s->capacity = cap;
}
bool push_dyn(DynStack *s, int value) {
if (s->top + 1 >= s->capacity) { // grow
int new_cap = s->capacity * 2;
int *grown = realloc(s->data, new_cap * sizeof(int));
if (!grown) return false;
s->data = grown;
s->capacity = new_cap;
}
s->data[++s->top] = value;
return true;
}
void destroy_dyn(DynStack *s) {
free(s->data);
s->data = NULL;
s->top = -1;
s->capacity = 0;
}</code></pre>
<p>Doubling the capacity when full gives O(1) amortized push time. Remember that every <code>malloc</code> must be matched by a <code>free</code>, and keep a destroy function so callers can release the memory. <a href="./array-stack">Array-backed stack design</a></p>
<h2>Linked List Stack in C</h2>
<p>To avoid resizing altogether, build the stack from nodes. Each node stores a value and a pointer to the next node, and the top of the stack is the head of the list.</p>
<pre><code>typedef struct Node {
int value;
struct Node *next;
} Node;
typedef struct {
Node *top;
} ListStack;
void init_list(ListStack *s) { s->top = NULL; }
bool push_list(ListStack *s, int value) {
Node *node = malloc(sizeof(Node));
if (!node) return false;
node->value = value;
node->next = s->top;
s->top = node;
return true;
}
bool pop_list(ListStack *s, int *value) {
if (!s->top) return false;
Node *old = s->top;
*value = old->value;
s->top = old->next;
free(old);
return true;
}</code></pre>
<p>Every push and pop is O(1) with no reallocation, but each node is a separate allocation with poorer cache locality. <a href="./linked-list-stack">Full linked list guide</a></p>
<h2>Memory Management</h2>
<p>C gives you full control of memory, which is powerful and also the source of most bugs. Follow these rules for a stack:</p>
<ul>
<li>Every <code>malloc</code> must eventually have a matching <code>free</code>.</li>
<li>Check the return value of <code>malloc</code> and <code>realloc</code>; they return NULL when they fail.</li>
<li>Free the stack's array in a destroy function before the program ends.</li>
<li>For linked stacks, free every node when destroying, not just the head.</li>
<li>Set freed pointers to NULL to avoid use after free.</li>
</ul>
<pre><code>void destroy_list(ListStack *s) {
Node *cur = s->top;
while (cur) {
Node *next = cur->next;
free(cur);
cur = next;
}
s->top = NULL;
}</code></pre>
<p><a href="./stack-memory">How stack memory is organized</a></p>
<h2>Complete C Stack Implementation</h2>
<p>Here is a full, compilable dynamic array stack program:</p>
<pre><code>#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef struct {
int *data;
int top;
int capacity;
} Stack;
void init(Stack *s, int cap) {
s->data = malloc(cap * sizeof(int));
s->top = -1;
s->capacity = cap;
}
bool push(Stack *s, int value) {
if (s->top + 1 >= s->capacity) {
s->capacity *= 2;
int *bigger = realloc(s->data, s->capacity * sizeof(int));
if (!bigger) return false;
s->data = bigger;
}
s->data[++s->top] = value;
return true;
}
bool pop(Stack *s, int *value) {
if (s->top < 0) return false;
*value = s->data[s->top--];
return true;
}
bool peek(const Stack *s, int *value) {
if (s->top < 0) return false;
*value = s->data[s->top];
return true;
}
void destroy(Stack *s) {
free(s->data);
s->data = NULL;
s->top = -1;
s->capacity = 0;
}
int main(void) {
Stack s;
init(&s, 4);
push(&s, 10);
push(&s, 20);
push(&s, 30);
int v;
while (pop(&s, &v)) {
printf("%d\n", v); // 30 20 10
}
destroy(&s);
return 0;
}</code></pre>
<p><a href="./guides">Explore more of the stack data structure</a></p>
<h2>Stack Overflow and Underflow Handling</h2>
<p>In C, overflow and underflow are not exceptions; they are silent bugs unless you handle them. If you push past the end of a fixed array, you overwrite adjacent memory. If you pop below the bottom, you read memory that does not belong to your stack.</p>
<p>The safe pattern is to have every push and pop return a boolean that reports success, and to check it in the calling code. This turns a memory corruption bug into a handleable failure, which is what production C code does.</p>
<p>Whether you check <code>top + 1 >= capacity</code> before push or let <code>realloc</code> grow the array, the goal is the same: never read or write outside the owned region. See the <a href="./">main visualizer</a> for stack code in five languages.</p>
<section class="callout">
<h2>Key Takeaway</h2>
<p>C gives you fine-grained control over a stack: a fixed array for known sizes, dynamic memory with realloc for growth, or a linked list when you never want to reallocate. Whatever you choose, manual memory management means you must free what you allocate and check overflow and underflow yourself.</p>
<p><a href="./guides">Explore all EasyStack guides</a></p>
</section>
<section class="faq" id="faq">
<h2>Frequently Asked Questions</h2>
<h3>Should I use a fixed array or dynamic memory for a C stack?</h3>
<p>Use a fixed array when the maximum stack size is known in advance and stays small, because it is simple, fast, and needs no allocation. Use dynamic memory with realloc when the stack must grow unpredictably, because that lets it scale without wasting memory in advance.</p>
<h3>What is stack overflow and underflow in a C stack?</h3>
<p>Stack overflow happens when you push onto a full stack: the top index reaches the capacity. Stack underflow happens when you pop from an empty stack: the top index is below zero. Both must be checked explicitly in C, because C does not throw exceptions or bounds-check arrays.</p>
<h3>How do I avoid memory leaks in a dynamic C stack?</h3>
<p>Free every block you malloc. For a dynamic array stack, call free() on the array in a destroy function. For a linked list stack, pop nodes and free each one until the list is empty. Use a tool like valgrind or AddressSanitizer to verify there are no leaks or invalid frees.</p>
</section>
</div>
</section>
</main>
<footer class="footer"><div class="shell"><div class="footer__grid">
<div><div class="footer__brand"><span class="brand__mark brand__mark--logo"><img src="./images/stack.gif" alt="" width="24" height="24"></span> EasyStack</div><p class="footer__about">A free, interactive stack data structure visualizer with comprehensive guides.</p><div class="footer-social"><a href="https://github.com/arundada9000" target="_blank" rel="noopener noreferrer" aria-label="GitHub"><img src="./images/stack-fav.png" alt="GitHub" style="width:19px;height:19px;border-radius:4px"></a></div></div>
<div><h4>Guides</h4><ul><li><a href="./stack-operations">Stack Operations</a></li><li><a href="./push-pop">Push and Pop</a></li><li><a href="./stack-complexity">Time Complexity</a></li><li><a href="./stack-analogies">Real-World Analogies</a></li><li><a href="./call-stack">The Call Stack</a></li></ul></div>
<div><h4>Learn</h4><ul><li><a href="./guides">All Guides</a></li><li><a href="./array-stack">Array Implementation</a></li><li><a href="./linked-list-stack">Linked List Stack</a></li><li><a href="./stack-memory">Stack Memory</a></li><li><a href="./monotonic-stack">Monotonic Stack</a></li></ul></div>
<div><h4>Site</h4><ul><li><a href="./about">About</a></li><li><a href="./contact">Contact</a></li><li><a href="./privacy">Privacy Policy</a></li><li><a href="https://github.com/arundada9000/Stack" target="_blank" rel="noopener">Source Code</a></li></ul></div>
</div><div class="footer__bottom"><span>Copyright <span data-year>2026</span> EasyStack. Made by Arun Neupane.</span><span>Free forever. No sign-up. Fully offline-ready PWA.</span></div></div></footer>
<script src="./hig.js"></script>
</body>
</html>