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 6360571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:39:55+00:00 2026-05-24T23:39:55+00:00

so I’m currently trying to migrate my Java experience to C++ by implementing various

  • 0

so I’m currently trying to migrate my Java experience to C++ by implementing various Data Structures for the sake of having them implemented at least once.

Would you mind giving me some advise? The problem I am having is mainly concentrated around the pointers in push(int value) and especially pop(). As push seems to be working correctly I found myself struggling to get the correct value back when pop’ing things. What’s the matter?

PS: I also think, that since I allocate my array space manually I’d need to delete it aswell. How do I do that?

#ifndef STACK_H
#define STACK_H

class Stack
{
private:
    int *stackArray;
    int elementsInArray;
    int allocatedArraySize;
    int alpha;
    int beta;

public:
    Stack();
    void push(int aValue);
    int pop();
    bool isEmpty();
    int size() const;
};

#endif

and the implementation:

#include <iostream>
#include "Stack.h"

Stack::Stack() 
{
    alpha = 4;
    beta = 2;
    elementsInArray = 0;
    allocatedArraySize = 1;
    stackArray = new int[1];
}

void Stack::push(int aValue)
{
    if (elementsInArray == allocatedArraySize) 
    {
        int temporaryArray[allocatedArraySize*beta];

        for (int i = 0; i < elementsInArray; i++)
            temporaryArray[i] = stackArray[i];

        stackArray = temporaryArray;
        allocatedArraySize *= beta;
    }

    elementsInArray++;
    stackArray[elementsInArray] = aValue;
}

int Stack::pop()
{
    int result = -INT_MAX;

    if (elementsInArray == 0)
        return result;

    if (elementsInArray > 0) 
    {
        result = stackArray[elementsInArray-1];
        elementsInArray--;

        if (elementsInArray <= allocatedArraySize/alpha) 
        {
            int temporaryArray[allocatedArraySize/alpha];

            for (int i = 0; i < elementsInArray; i++) 
                temporaryArray[i] = stackArray[i];

            stackArray = temporaryArray;
            allocatedArraySize /= beta;
        }
    }

    return result;
}

bool Stack::isEmpty()
{
    if (elementsInArray == 0) 
        return true;

    return false;
}

int Stack::size() const
{
    return allocatedArraySize;
}
  • 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-24T23:39:55+00:00Added an answer on May 24, 2026 at 11:39 pm

    For starters, you should be post incrementing the index on the array, so change:

    elementsInArray++;
    stackArray[elementsInArray] = aValue;
    

    to:

    stackArray[elementsInArray++] = aValue;
    

    or:

    stackArray[elementsInArray] = aValue;
    elementsInArray++;
    

    Second, when you create the new temp array you are doing it inside the if statement… therefore it is a local variable and placed on the system stack and lost after you exit the if statement. So change

    int temporaryArray[allocatedArraySize*beta];
    

    to:

    int *temporaryArray = new int[allocatedArraySize*beta];
    

    Third, add in the delete you were talking about by saving the original pointer from stackArray before copying the location of tempArray and then perform the delete after you’ve made the pointer copy.

    Finally, you’ll have to make similar changes to your pop function…

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I am currently running into a problem where an element is coming back from
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I am trying to loop through a bunch of documents I have to put
I have some data like this: 1 2 3 4 5 9 2 6
I need to clean up various Word 'smart' characters in user input, including but

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.