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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:04:54+00:00 2026-06-15T06:04:54+00:00

The following pertains to homework. Restraunt pet project type thing, task is to update

  • 0

The following pertains to homework. Restraunt pet project type thing, task is to update it to use vectors. The issue I’m having is this:
This winds up causing a core segmentation fault, yet is able to retrieve all the information appropriately when I use valgrind.

void Table::partyCheckout(void)
{
        if(status == SERVED)
        {
                cout << " ---------------- " << endl;
                cout <<"Table: " << tableId << "\nParty Size: " << numPeople << "\nWaiter: " << waiter->getName() << "\nSummary: " <<endl;
                order->requestSummary();
                cout << "Total: " << endl;
                order->requestTotal();
                cout << " ---------------- " << endl;
                status = IDLE;
        }
        else
        {
                cout << "Error: " << tableId << " ";
                if(numPeople == 0)
                {
                        cout << "No one is at this table." << endl;
                }
                else
                {
                        cout << "This party hasn't been served." << endl;
                }
        }
}

Setup: I’m storing the waiters and the orders in vectors.
At runtime: when it does the waiter->getName() it complains that it’s an invalid read, and that the memory location has been free’d by vector via a deallocater.
My logic on the matter: It looks ahead and sees that the vector itself is not accessed again and so deallocates it. Since I do no more writing after this point, the memory location remains intact. When it tries to read the location it sees it has been free’d, hence invalid read but it still gets the appropriate data.
So my question then, I suppose is two fold:
Does this logic sound right?
What should I do to fix it?

#ifndef HW3_H
#define HW3_H
#include <vector>
#include "Table.h"
#include "Waiter.h"

class hw3
{
private:

        vector<Table> tables;
        vector<Waiter> waiters;
        vector<Order> orders;
public:
        void begin();

};
#endif

.cpp file, most of the allocation:

 ifstream configFile("config.txt"); //This guy is for initializing things
        string line;

        Menu theMenu;
        getline(configFile, line);
        stringstream intMaker;
        int t1;
        int t2;
        string temp;
        string temp2;
        string temp3;
        while (true)
        {
                getline(configFile, line);
                Tokenizer str(line, " \n");
                if(line =="")
                {


                        break;
                }
                else
                {
                        temp = str.next();
                        temp2 = str.next();
                        intMaker << temp;
                        intMaker >> t1;
                        intMaker.str("");
                        intMaker.clear();
                        intMaker << temp2;
                        intMaker >> t2;
                        intMaker.str("");
                        intMaker.clear();
                        tables.push_back(*(new Table(t1,t2)));


                }


        }

        getline(configFile, line);
        while (true)
        {
                getline(configFile, line);
                Tokenizer name(line, " ");
                string tabl = "";
                //Siphon off the name and the tables.
                temp = name.next();
                tabl = name.next();
                Tokenizer strink(tabl, ",\n");
                int numTables = (int) tables.size();
                Table * tabs[numTables];
                t1 = 0;
                int keepinTabs = 0;
                while(true)
                {
                        string temp2 = strink.next();

                        if (temp2 == "")
                        {

                                 break;
                        }
                        else
                        {
                                 intMaker << temp2;
                                 intMaker >> t1;

                                 intMaker.str("");
                                 intMaker.clear();
                                 for(int i = 0; i < numTables; i++)
                                 {
                                        if(tables.at(i).getTableId() == t1)
                                        {
                                                tabs[keepinTabs] = &tables.at(i);
                                        }
                                 }
                                 keepinTabs++;
                        }

                }

                waiters.push_back(*(new Waiter(temp, tabl, *tabs))); //Waiter(name, list of tables, and an array of table numbers.
                for(int j = 0; j < keepinTabs; j++)
                {
                        for(int i = 0; i < tables.size(); i++)
                        {
                                if(tabs[j]->getTableId() == tables[i].getTableId())
                                {
                                        tables.at(i).assignWaiter(&(waiters.back()));
                                }
                        }
                }
                if(line == "")
                {
                        break;
                }
        }
  • 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-15T06:04:55+00:00Added an answer on June 15, 2026 at 6:04 am

    Multiple issues I can see:

    tables.push_back(*(new Table(t1,t2)));
    

    This code dynamically allocates an object of type Table, then pushes a copy of this object into tables, and then forgets the address of the dynamically allocated object – you’re leaking memory.

    waiters.push_back(*(new Waiter(temp, tabl, *tabs)));
    

    As above, with Waiter this time.

    tabs[keepinTabs] = &tables.at(i);
    

    This takes the address of an object inside the vector. While legal, it’s extremely fragile. std::vector can move its contents around in memory when it resizes (e.g. when you push into it).

    This (or similar code elsewhere) might be the cause of your segfault. Seeing as you’re allocating the objects dynamically, maybe you should declare your vectors to hold just pointers:

    vector<Table*> tables;
    vector<Waiter*> waiters;
    vector<Order*> orders;
    

    You would then do e.g. tables.push_back(new Table(t1, t2));. Of course, you have to make sure to delete the dynamically allocated objects when you remove them from the vectors. An alternative would be to use smart pointers, e.g.:

    vector<std::shared_ptr<Table> > tables;
    vector<std::shared_ptr<Waiter> > waiters;
    vector<std::shared_ptr<Order> > orders;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I hardly see any pointer on the following problem related to Hibernate. This pertains
Following on from this question: Can't I use JQuery inside my FancyZoom popup? The
Following this tutorial http://msdn.microsoft.com/en-us/vstudio/hh543922.aspx , I'm trying to use the ReplaceNode method that should
Following leads in this thread and others, I've set up a code block where
The following situation pertains to an iPad app I am working on for my
This pertains to C# & the .NET Framework specifically. It's best practice not to
I have the following JavaScript for show running line: <script type=text/javascript language=javascript> //Change script's
I have a project that is coming around the bend this summer that is
To all: This question pertains to some MS research I am doing. What I
Following this question: Good crash reporting library in c# Is there any library like

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.