Does Stack Data Structure Have Fixed Size?


Angela Bailey

Stack Data Structure is a fundamental concept in computer science and is widely used in various applications. One of the frequently asked questions about stack is whether it has a fixed size or not. In this article, we will explore this question in detail and understand the characteristics of stack data structure.

What is a Stack Data Structure?

A stack is an abstract data type that follows the LIFO (Last-In-First-Out) principle. It resembles a real-life stack of objects, where the last item added to the stack is the first one to be removed.

In a stack, two main operations can be performed:

  • Push: This operation adds an element to the top of the stack.
  • Pop: This operation removes and returns the element from the top of the stack.

Fixed Size vs. Dynamic Size

The size of a stack can vary depending on its implementation. In some cases, a stack may have a fixed size, while in others, it may have a dynamic size.

Fixed Size Stack

A fixed size stack has a predetermined maximum capacity that cannot be exceeded. Once this capacity is reached, no more elements can be pushed into the stack until some elements are popped out to free up space.

The advantage of using a fixed size stack is that it provides better memory management and avoids potential memory overflow issues. However, one must carefully choose an appropriate size for the fixed-size stack to ensure it can accommodate all possible elements without wasting memory.

Dynamic Size Stack

A dynamic size stack, on the other hand, can grow or shrink as needed based on runtime conditions. It does not have any predefined limit on the number of elements it can hold.

The advantage of using a dynamic size stack is its flexibility, as it can adapt to changing requirements. However, it requires additional memory management overhead and may be prone to memory overflow if not properly handled.

Implementing Stack with Fixed Size

To implement a fixed size stack, we typically use an array as the underlying data structure. The size of the array determines the maximum capacity of the stack.

Here’s an example of how a fixed size stack can be implemented in C++:

#include <iostream>
using namespace std;

const int MAX_SIZE = 100; // Maximum capacity of the stack

class Stack {
  int arr[MAX_SIZE];
  int top;
  Stack() {
    top = -1;
  void push(int value) {
    if (top == MAX_SIZE - 1) {
      cout << "Stack Overflow!" << endl;
    arr[++top] = value;
  int pop() {
    if (top == -1) {
      cout << "Stack Underflow!" << endl;
      return -1; // Assuming -1 as an error code
    return arr[top--];

int main() {
  Stack stack;
  cout << "Popped element: " << stack.pop() << endl;

  return 0;

In this example, we define a class called Stack that uses an integer array to store elements. The push() function adds elements to the top of the stack, and the pop() function removes and returns the top element. If the stack is full or empty, appropriate error messages are displayed.


So, does a stack data structure have a fixed size? The answer is that it can have both a fixed size and a dynamic size, depending on how it is implemented. A fixed size stack has a predetermined maximum capacity, while a dynamic size stack can grow or shrink as needed.

Understanding the characteristics of stack data structure and its sizing options is essential for designing efficient algorithms and solving various programming problems.

Happy coding!

Discord Server - Web Server - Private Server - DNS Server - Object-Oriented Programming - Scripting - Data Types - Data Structures

Privacy Policy