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

  • Home
  • SEARCH
  • 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 8702595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:41:29+00:00 2026-06-13T02:41:29+00:00

I am trying to write a program to calculate postfix expressions, it includes both

  • 0

I am trying to write a program to calculate postfix expressions, it includes both a driver and an implementation file. These two shown below:

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

using namespace std;

void printStack(Stack<float>);

int main()
{
 Stack<float> postFix;
 float userIn, operand1, operand2;
 // while the end of the postfix hasn't been reached
 do
 { 
   // prompt user for next number or modulus command
   cout << "ENTER: ";
   if (!postFix.isEmpty()) printStack(postFix);
   string userInput;
   cin >> userInput;

   // If user input is q or Q then terminate program
   if (userInput == "q" || userInput == "Q") return 0;
   else
   {
     // read next token by parsing userInput
     // if token is a number push it onto stack
  if (userInput != "+" && userInput != "/" && userInput != "*" && userInput != "-")
  {
    userIn = atof(userInput.c_str());
    postFix.push(userIn);
  }
  else if (!(postFix.size() < 2))
  {
    // if it's not a number then pop element from the stack and call it
    // operand2
    operand2 = postFix.lastElem();
    postFix.pop();

    // pop another element from the stack; call it operand1
    operand1 = postFix.lastElem();
    postFix.pop();

    // Perform the operation: operand1 token operand2 and
    // push the result of the operation onto the stack
    if (userInput == "/")
      postFix.push((operand1 / operand2));
    else if (userInput == "*")
      postFix.push((operand1 * operand2));
    else if (userInput == "+")
      postFix.push((operand1 + operand2));
    else if (userInput == "-")
      postFix.push((operand1 - operand2));
  }
  else if (postFix.size() < 2)
    cout << "Stack is too small, request ignored\n";
} // end else

} while (!postFix.isEmpty()); // end while

// empty the stack
postFix.makeEmpty();

return 0;
}

// The problem is in the print function!

void printStack(Stack<float> postFix)
{
  do
  {
    cout << postFix.lastElem() << " ";
    postFix.pop();
  } while (!postFix.isEmpty());
}

My implementation file is shown here:

#include <iostream>
using namespace std;

template <class DataType>
class Stack
{
  public:
  Stack() // constructor
  {
    capacity = 2;
    //Stack(capacity);
    elements = new DataType[capacity];
    top = -1;
  }

  Stack(int s) // constructor with one parameter
  {
    capacity = s;
    elements = new DataType[capacity];
    top = -1;
  }

~Stack()
{
  delete [] elements;
}

bool isEmpty() const {return -1 == top;}

void pop() // pop function
{
  //if (-1 == top) return; // failed
  top--;
  if (top > 2 && top < (capacity / 4))
  {
    capacity /= 2;
    changeSize(capacity);
  }
}

DataType & lastElem()
{
  return elements[top];
}

int size() {return top;}

void push(float parameter) // push function
{
  if (++top == capacity)
  {
    capacity *= 2;
    changeSize(capacity);
  }
  elements[top] = parameter;
}

bool peek(DataType& parameter) const
{
  if (-1 == top) return false; // failed
  parameter = elements[top];
  return true; // success
}

inline DataType & operator=(const Stack<DataType>& a)
{
  if (this != &a)
  {
    delete [] elements;
    elements = 0;
    capacity = a.capacity;
    if (capacity > 0)
      elements = new DataType[capacity];
    for (int i = 0; i < capacity; i++)
      elements[i] = a.elements[i];
    top = a.top;
  }
  return *this;
}

void changeSize(float newSize) // function for changing the array size if it's too small
{
  DataType *newArray = new DataType[(int)newSize];
  int limit = (newSize > capacity)? capacity : newSize;

  for (int i = 0; i < limit; i++)
  {
    newArray[i] = elements[i];
  }
  delete [] elements;
  elements = newArray;
  capacity = newSize;
}

void makeEmpty() {top = -1;}

private:
  DataType* elements;
  int capacity;
  int top; // track newest value 
};

After I compile this program using g++, I can enter up to two values until I get a strange memory error shown below:

ENTER: 23
ENTER: 23 43
*** glibc detected *** ./calc: double free or corruption (fasttop): 0x0000000000c86010 ***
======= Backtrace: =========
/lib/libc.so.6(+0x78b06)[0x7fbd11e4ab06]
/lib/libc.so.6(cfree+0x73)[0x7fbd11e513d3]
./calc[0x401197]
./calc[0x400d9b]
/lib/libc.so.6(__libc_start_main+0xfd)[0x7fbd11df0c4d]
./calc[0x400c09]
======= Memory map: ========
00400000-00402000 r-xp 00000000 08:06 788295                             /home/badr/Documents/My programs/COMSC-210/calc
00601000-00602000 r--p 00001000 08:06 788295                             /home/badr/Documents/My programs/COMSC-210/calc
00602000-00603000 rw-p 00002000 08:06 788295                             /home/badr/Documents/My programs/COMSC-210/calc
00c86000-00ca7000 rw-p 00000000 00:00 0                                  [heap]
7fbd0c000000-7fbd0c021000 rw-p 00000000 00:00 0 
7fbd0c021000-7fbd10000000 ---p 00000000 00:00 0 
7fbd11dd2000-7fbd11f4f000 r-xp 00000000 08:06 5813                       /lib/libc-2.11.1.so
7fbd11f4f000-7fbd1214e000 ---p 0017d000 08:06 5813                       /lib/libc-2.11.1.so
7fbd1214e000-7fbd12152000 r--p 0017c000 08:06 5813                       /lib/libc-2.11.1.so
7fbd12152000-7fbd12153000 rw-p 00180000 08:06 5813                       /lib/libc-2.11.1.so
7fbd12153000-7fbd12158000 rw-p 00000000 00:00 0 
7fbd12158000-7fbd1216e000 r-xp 00000000 08:06 1268                       /lib/libgcc_s.so.1
7fbd1216e000-7fbd1236d000 ---p 00016000 08:06 1268                       /lib/libgcc_s.so.1
7fbd1236d000-7fbd1236e000 r--p 00015000 08:06 1268                       /lib/libgcc_s.so.1
7fbd1236e000-7fbd1236f000 rw-p 00016000 08:06 1268                       /lib/libgcc_s.so.1
7fbd1236f000-7fbd123f1000 r-xp 00000000 08:06 5814                       /lib/libm-2.11.1.so
7fbd123f1000-7fbd125f0000 ---p 00082000 08:06 5814                       /lib/libm-2.11.1.so
7fbd125f0000-7fbd125f1000 r--p 00081000 08:06 5814                       /lib/libm-2.11.1.so
7fbd125f1000-7fbd125f2000 rw-p 00082000 08:06 5814                       /lib/libm-2.11.1.so
7fbd125f2000-7fbd126e8000 r-xp 00000000 08:06 322753                     /usr/lib/libstdc++.so.6.0.13
7fbd126e8000-7fbd128e8000 ---p 000f6000 08:06 322753                     /usr/lib/libstdc++.so.6.0.13
7fbd128e8000-7fbd128ef000 r--p 000f6000 08:06 322753                     /usr/lib/libstdc++.so.6.0.13
7fbd128ef000-7fbd128f1000 rw-p 000fd000 08:06 322753                     /usr/lib/libstdc++.so.6.0.13
7fbd128f1000-7fbd12906000 rw-p 00000000 00:00 0 
7fbd12906000-7fbd12926000 r-xp 00000000 08:06 5815                       /lib/ld-2.11.1.so
7fbd12afa000-7fbd12afe000 rw-p 00000000 00:00 0 
7fbd12b21000-7fbd12b25000 rw-p 00000000 00:00 0 
7fbd12b25000-7fbd12b26000 r--p 0001f000 08:06 5815                       /lib/ld-2.11.1.so
7fbd12b26000-7fbd12b27000 rw-p 00020000 08:06 5815                       /lib/ld-2.11.1.so
7fbd12b27000-7fbd12b28000 rw-p 00000000 00:00 0 
7fff78cf3000-7fff78d08000 rw-p 00000000 00:00 0                          [stack]
7fff78dff000-7fff78e00000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
ENTER: 43 0 Aborted

After much debugging I have determined that the problem must be in the print function located in the .cpp file:

// The problem is in the print function!

void printStack(Stack<float> postFix)
{
  do
  {
    cout << postFix.lastElem() << " ";
    postFix.pop();
  } while (!postFix.isEmpty());
}

I just can’t figure out why it’s happening or how to overcome it.

  • 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-06-13T02:41:31+00:00Added an answer on June 13, 2026 at 2:41 am

    void printStack(Stack<float> postFix) takes the parameter by value, so a copy is made. Using the compiler-generated copy constructor. Which does a shallow copy. You’ll need to implement your own, that does a deep-copy.

    You already have a copy assignment operator (operator =) and a destructor. Which is correct, but not enough. These 2 plus the copy constructor Stack(const Stack<DataType>& other) make up the rule of three. If you need one of them, you need them all. And usually, you need them all when you’re managing memory in the class, which you are.

    The implementation is similar to that of operator =:

    DataType(const Stack<DataType>& a)
    {
        elements = 0;
        capacity = a.capacity;
        if (capacity > 0)
          elements = new DataType[capacity];
        for (int i = 0; i < capacity; i++)
          elements[i] = a.elements[i];
        top = a.top;
    }
    

    Also, just in case this isn’t for educational purposes, there is a pre-implemented std::stack in the header <stack> which you can use.

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

Sidebar

Related Questions

new to python here. I am trying to write a program that calculate the
I trying to write a program that navigates the local file system using a
Im trying to write a program to read a text file through args but
I'm trying to write a simple program to calculate betweeness using brandes_betweenness_centrality from boostlib.
I am trying to write a program in C to calculate the remaining loan
I am trying to write a program to collect security information about a file
I'm trying to write a program that determines whether two words are cognates. I've
I am trying to write a Java program to calculate factorial of a large
I am trying write a program in python to calculate heart rate training zones
I am trying to write a program using opencv to calculate the distance from

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.