Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6965507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:04:13+00:00 2026-05-27T16:04:13+00:00

i write a stack class in c++ (shown below) but it is static and

  • 0

i write a stack class in c++ (shown below) but it is static and sure it uses lots of memory’s. how can i make it dynamic so when ever it need it add some memory to the object and when ever i pop something the memory automatically delete?

template <class T>
class stack
{
private:
    T value[512];
    uint16_t length;
public:
    stack()
    {
        length=0;
    }

    stack(T _input)
    {
        value[0]=_input;
        length=1;
    }

    bool push(T _input)
    {
        if(length+1<512)
        {
            value[++length]=_input;
            return true;    
        }
        else
            return false;
    }

    T pop()
    {
        return value[length--];     
    }

    T peak()
    {
        return value[length];   
    }

    bool has_data()
    {
        return (length>0?true:false);
    }

};
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T16:04:13+00:00Added an answer on May 27, 2026 at 4:04 pm

    You have to allocate it dynamically when the need arises. Something like this maybe:

    #define STACK_INITIAL_ALLOC   32
    #define STACK_CHUNK_ALLOC    32
    
    template<typename T>
    class Stack
    {
    public:
        Stack()
            : data(0), entries(0), allocated(0)
            { }
    
        Stack(const T &value)
            : data(0), entries(0), allocated(0)
            {
                push(input);
            }
    
        ~Stack()
            {
                if (data)
                    delete [] data;
            }
    
        void push(const T &value)
            {
                if (entries == allocated)
                    allocate();  // Allocate more memory
    
                data[entries++] = value;
            }
    
        T pop()
            {
                if (entries > 0)
                {
                    shrink();
                    return data[--entries];
                }
                else
                    throw runtime_error("stack empty");
            }
    
        T &top()
            {
                if (entries > 0)
                    return data[entries - 1];
                else
                    throw runtime_error("stack empty");
            }
    
        // Return the number of entries in the stack
        size_t count() const
            {
                return entries;
            }
    
    private:
        T      *data;      // The actual stack
        size_t  entries;   // Number of entries in stack
        size_t  allocated; // Allocated entries in stack
    
        void copy(T *from, T *to)
            {
                for (size_t i = 0; i < entries; i++)
                    *to++ = *from++
            }
    
        void allocate()
            {
                if (data == 0)
                {
                    allocated = STACK_INITIAL_ALLOC;
                    data = new T[allocated];
                }
                else
                {
                    // We need to allocate more memory
    
                    size_t new_allocated = allocated + STACK_CHUNK_ALLOC;
                    T *new_data = new T[new_allocated];
    
                    // Copy from old stack to new stack
                    copy(data, new_data);
    
                    // Delete the old data
                    delete [] data;
    
                    allocated = new_allocated;
                    data = new_data;
                }
            }
    
        // Shrink the stack, if lots of it is unused
        void shrink()
            {
                // The limit which the number of entries must be lower than
                size_t shrink_limit = allocated - STACK_CHUNK_ALLOC * 2;
    
                // Do not shrink if we will get lower than the initial allocation (shrink_limit > STACK_INITIAL_ALLOC)
                if (shrink_limit > STACK_INITIAL_ALLOC && entries < shrink_limit)
                {
                    // We can shrink the allocated memory a little
                    size_t new_allocated = allocated - STACK_CHUNK_ALLOC;
    
                    T *new_data = new T[new_size];
    
                    copy(data, new_data);
    
                    delete [] data;
    
                    data = new_data;
                    allocated = new_allocated;
                }
            }
    };
    

    Also a small disclaimer, this code was written straight into the browser. It’s not tested, but should work in principle… 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write an app using GtkClutter but I can't get the actors
How do I write a Windows 7 Search Connector for Stack Overflow?
I want to write a script for gdb, which will save backtrace (stack) of
In this book, I am learning how the book writes a stack, but when
I'm stuck in having to write a simple spam filter I'm not really sure
I hope someone can guide me as I'm stuck... I need to write an
Question : b) A Stack is a last-in first-out (LIFO) data structure. Write a
Spring MVC web app: I have a stack trace w/o line numbers (shown at
I have a static class which is accessed by multiple remoting and other internal
I am having a class which uses the XmlReader and XmlReaderSettings classes in C#

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.