Losing a data member of a base class after the constructor is called - c++

I have been working on a project and had no problems until today where I lost a class data member after the constructor is called. I could not pinpoint where the change happens. I have several classes for cars. The classes related to the cars are in the code block below. After all the cars are created in the class RegistrationSystem they are stored in an array. however, while calling a display function in RegistrationSystem the first car loses the color value. In the class RegistrationSystem the first Pickup in the class stores the value until the constructor is complete. After the constructor ends, the color value string shows " " in the debugger.
class Car {
protected:
string color;
public:
Car(string c) { color = c; };
string getColor(void) { return color; }
friend ostream& operator<<(ostream& outStream, const Car& car) {
cout << car.color;
return outStream;
}
};
class Pickup : public Car {
public:
Seat *frontSeat = new Seat(FRONT_SEAT_CREDIT);
Pickup(string c) : Car(c) {}
};
class Compact : public Car {
public:
Seat *frontSeat = new Seat(FRONT_SEAT_CREDIT);
Seat *sideBackLeftSeat = new Seat(BACK_SEAT_COMPACT_CREDIT);
Seat *sideBackRightSeat = new Seat(BACK_SEAT_COMPACT_CREDIT);
Compact(string c) : Car(c) {}
};
class Sedan : public Car {
public:
Seat *frontSeat = new Seat(FRONT_SEAT_CREDIT);
Seat *sideBackLeftSeat = new Seat(SIDE_BACK_SEDAN_CREDIT);
Seat *sideBackRightSeat = new Seat(SIDE_BACK_SEDAN_CREDIT);
Seat *middleBackSeat = new Seat(MID_BACK_SEDAN_CREDIT);
Sedan(string c) : Car(c) {}
};
class RegistrationSystem {
private:
string file_name;
int menu_response;
Reservation *reservations[24] = { NULL };
Passenger *rowers[24];
Pickup *pickup_cars[3];
Compact *compact_cars[3];
Sedan *sedan_cars[3];
// Displays the Layouts
void displaySeatArrangements(void);
// Saves the information in the file
void saveToFile(void);
// Find the rower in array rowers
Passenger* findRower(string);
// Displays the menu for the seat choice of a car type
bool displayCarSeatingChoiceMenu(string);
// Make a reservation in the system
bool makeReservation(string, string, int, Passenger&);
// Delete a reservation
bool deleteReservation(int);
// Print Reservations
void saveReservationsToFile(void);
// Sub functions for makeReservation()
bool makePickupReservation(Pickup*, Passenger, int&, string, string);
bool makeCompactReservation(Compact*, Passenger, int&, string, string);
bool makeSedanReservation(Sedan*, Passenger, int&, string, string);
public:
RegistrationSystem(void);
void chooseOperation(void);
Passenger* getPassengers(void) { return *rowers; }
friend ostream& operator<<(ostream&, const RegistrationSystem&);
friend istream& operator>>(istream&, RegistrationSystem&);
};
The display function has a line:
<< setw(8) << *(pickup_cars[index]) << setw(8) << *(compact_cars[index]) << setw(11) << *(sedan_cars[index]) << endl
where the *(pickup_cars[index]) is set to " " and the seat value gives error = read memory from 0x6 failed (0 of 4 bytes read). That is also set to NULL.
Here is the code of the RegistrationSystem constructor:
RegistrationSystem::RegistrationSystem(void) {
file_name = "seat_credits.txt";
menu_response = 0;
ifstream inFile(file_name);
if(!inFile.is_open()) {
cout << "Error opening file. Terminating...";
exit(1);
}
// Read file to set passengers and their credits in rowers array
int count = 0;
while(!inFile.eof() && count < 24) {
string first, last;
int credits;
inFile >> first >> last >> credits;
string fullName = first + ' ' + last;
rowers[count] = new Passenger(fullName, credits);
count++;
}
// Assign all the cars to the arrays
pickup_cars[0] = new Pickup("PURPLE");
pickup_cars[1] = new Pickup("YELLOW");
pickup_cars[2] = new Pickup("RED");
compact_cars[0] = new Compact("GREEN");
compact_cars[1] = new Compact("BLUE");
compact_cars[2] = new Compact("YELLOW");
sedan_cars[0] = new Sedan("RED");
sedan_cars[1] = new Sedan("GREEN");
sedan_cars[2] = new Sedan("BLUE");
inFile.close();
}

The problem occurred when the input file stream was still open and I was creating new instances and assigning their addresses to pointers before I closed the file. After I switched the order and closed the file, then assigned pointers the addresses of the new objects the problem went away.
These operations were happening inside of the constructor of the RegistrationSystem class. Here is the new code:
RegistrationSystem::RegistrationSystem(void) {
file_name = "seat_credits.txt";
menu_response = 0;
ifstream inFile(file_name);
if(!inFile.is_open()) {
cout << "Error opening file. Terminating...";
exit(1);
}
// Read file to set passengers and their credits in rowers array
int count = 0;
while(!inFile.eof() && count < 24) {
string first, last;
int credits;
inFile >> first >> last >> credits;
string fullName = first + ' ' + last;
rowers[count] = new Passenger(fullName, credits);
count++;
}
inFile.close();
// Assign all the cars to the arrays
pickup_cars[0] = new Pickup("PURPLE");
pickup_cars[1] = new Pickup("YELLOW");
pickup_cars[2] = new Pickup("RED");
compact_cars[0] = new Compact("GREEN");
compact_cars[1] = new Compact("BLUE");
compact_cars[2] = new Compact("YELLOW");
sedan_cars[0] = new Sedan("RED");
sedan_cars[1] = new Sedan("GREEN");
sedan_cars[2] = new Sedan("BLUE");
}
The line inFile.close() was placed after all the array assignments. Moving it above them solved the problem of losing the first data member of my Pickup object.

Related

Std::bad_alloc thrown in the middle of While Loop

I'm writing a function that handles an order input file (csv) using a while loops to iterate though it.
762212,1,2020-03-15,10951-3,64612-2,57544-1,80145-1,27515-2,16736-1,79758-2,29286-2,51822-3,39096-1,32641-3,63725-3,64007-2,23022-1,16974-3,26860-2,75536-2,26461-1
1,373975319551257,12-2023
258572,2,2020-03-15,96497-1,70616-1,80237-2,22248-2,56107-1,59695-1,37948-3,21316-3,63498-1,18329-1,56833-1,66295-1,47680-3,30346-1
1,201741963232463,02-2022
857003,3,2020-03-15,16655-1,88019-3,75069-3,96017-2,46883-2,15138-1,77316-1,70063-3,54452-3,86429-2,15134-2,60176-1,12946-3
2,cfeeham3s
747893,4,2020-03-17,48520-1,93268-2,63636-1,23750-2,99771-3,83203-1,21316-3,89921-2,15134-3,82831-1,30346-2,54044-3,28561-1,14792-2,23523-3,56826-2
1,3571379825697064,04-2025
Every two lines represents an input. I have the following function that handles this input:
list<Order> orders;
void read_orders(string file_name) {
fstream read_file;
read_file.open(file_name, ios::in);
if (read_file.is_open()) {
string s;
int line_num = 1; // keeps track of line number in input file
int o_id;
string o_date;
int c_id;
vector<LineItem> itms;
while (getline(read_file, s)) {
cout << orders.size(); // shows that only two objects are added before failure
if (line_num % 2 == 1) { // handle odd numbered lines of input
auto data = split(s, ',');
int o_id = stoi(data[0]);
string o_date = data[1];
int c_id = stoi(data[2]);
vector<LineItem> itms;
// get line items
int n_line_items = data.size() - 3;
vector<string> end_data(data.end() - n_line_items, data.end());
for (string x: end_data) {
auto parts = split(x, '-');
LineItem* it = new LineItem(stoi(parts[0]), stoi(parts[1]));
itms.push_back(*it);
delete it;
}
} else { // handle even numbered lines of input
auto data = split(s, ',');
Credit* pay_credit = new Credit(0.0, data[1], data[2]); // initialize each type of payment
PayPal* pay_paypal = new PayPal(0.0, data[1]);
WireTransfer* pay_wire = new WireTransfer(0.0, data[1], data[2]);
if (data[0] == "1") {
Order* ordr = new Order(o_id, o_date, c_id, itms, *pay_credit);
orders.push_back(*ordr);
delete ordr;
} else if (data[0] == "2") {
Order* orr = new Order(o_id, o_date, c_id, itms, *pay_paypal);
orders.push_back(*orr);
delete orr;
} else if (data[0] == "3") {
Order* odr = new Order(o_id, o_date, c_id, itms, *pay_wire);
orders.push_back(*odr);
delete odr;
}
delete pay_credit; // trying to clean up memory
delete pay_paypal;
delete pay_wire;
}
line_num += 1;
}
read_file.close();
}
}
Because of my cout statement, I can tell that it only adds two items to the list before running into the std::bad_alloc error. It seems to happen when it switches from adding a Credit object to adding a PayPal object into the Order(...) when it's initialized. I did a lot of research into why this might happen, so I tried to clean up as much as I knew how to (I'm new to C++) but the same error kept popping up. Does the error happen when I'm adding things to the list or is it when I'm creating these new objects?/How could I fix something like that?
Here are my class definitions in case that's important:
class Payment {
public:
double amount;
string print_detail() {
return "hey";
};
};
class Credit: public Payment {
private:
string card_number;
string expiration;
public:
Credit(double amt, string cn, string exp) {
this->amount = amt;
this->card_number = cn;
this->expiration = exp;
}
string print_detail() {
return "Credit card " + this->card_number + ", exp. " + this->expiration;
}
};
class PayPal: public Payment {
private:
string paypal_id;
public:
PayPal(double amt, string pp_id) {
this->amount = amt;
this->paypal_id = pp_id;
}
virtual string print_detail() {
return "Paypal ID: " + this->paypal_id;
}
};
class WireTransfer: public Payment {
private:
string bank_id;
string account_id;
public:
WireTransfer(double amt, string b_id, string a_id) {
this->amount = amt;
this->bank_id = b_id;
this->account_id = a_id;
}
string print_detail() {
return "Wire transfer from Bank ID " + this->bank_id + ", Account# " + this->account_id;
}
};
class LineItem {
private:
int item_id;
int qty;
public:
LineItem(int i_id, int qt) {
this->item_id = i_id;
this->qty = qt;
}
double subtotal() {
double subtot = 0.0;
for (auto x: items) {
if (x.item_id == this->item_id) {
subtot += x.price * this->qty;
}
}
return subtot;
};
};
class Order {
private:
int order_id;
string order_date;
int cust_id;
vector<LineItem> line_items;
Payment payment;
public:
Order(int o_id, string o_date, int c_id, vector<LineItem> li, Payment pay) {
this->order_id = o_id;
this->order_date = o_date;
this->cust_id = c_id;
this->line_items = li;
this->payment = pay;
}
string pay_type = "";
double total() {
double result = 0.0;
for (auto li: line_items) {
result += li.subtotal();
}
return result;
}
string print_order() {
string text = "===========================\nOrder #";
text += to_string(this->order_id) + ", Date: " + this->order_date + "\nAmount: $";
text += to_string(this->total()) + ", Paid by ";
text += payment.print_detail();
return text;
}
};
And this was the error message showing that it did insert two items:
001122terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
Process returned 3 (0x3)
std::bad_alloc is often thrown when there is not enough memory to be allocated. I can't say if this will solve the problem, but your repeated allocations and deallocations of objects are both unnecessary and harmful (causing memory fragmentation).
Instead of
LineItem* it = new LineItem(stoi(parts[0]), stoi(parts[1]));
itms.push_back(*it);
delete it;
you should do
itms.push_back(LineItem(stoi(parts[0]), stoi(parts[1]));
or
itms.emplace_back(stoi(parts[0]), stoi(parts[1]));
The same applies to every occurence of new in read_orders. You don't need any of them.
Another helpful thing you can do is to preallocate memory for std::vector. If you don't know how many items it will have, do an educated guess (100, 1000, 10000, etc.).
itms.reserve(1000); //before you start to push_back() to it
Also, make sure to std::move your vectors if you want to transfer the whole content of it and not make a copy.

Store the address of an object inside a node

I'm trying to create an object of a class called Cell and store it in a linked list. I'm sure I could do this with an array, but part of my assignment is that I use a linked list and I didn't think I'd get this many problems. This is currently my node. Right now, I have all these variables stored in the node, but I'd rather create an object(Called "Cell") to store them. Info should be a pointer to an object of type T. Right now, that T should be of type Cell.
template<class T>
struct Node {
T *info;
Node<T> *nodeP;
Node<T> *linkP;
int nodeNumber = 0;
bool purchased = false;
std::string color = " ";
int index = 0;
int max_num = 0;
std::string name = " ";
int price;
};
In here I am creating the node and adding it to a linked list. At the moment I'm just filling in values of the node, but I'm trying to create an object of type Cell and assign it's address to the pointer info. I've tried a couple different ways but keep coming back with errors. I commented them out so you can see what I've tried.
template<class T>
void Board<T>::setCellValue() {
//open file
ifstream inFile;
string line;
inFile.open("CellValues.txt");
//Check for Error
if (inFile.fail()) {
cerr << "File does not exist!";
exit(1);
}
int index = 0, max_num = 0, count = 0, price = 0;
string color, name;
istringstream inStream;
while (getline(inFile, line)) {
inStream.clear();
inStream.str(line);
inStream >> color >> index >> max_num >> name >> price;
//creates node
Node<T> *newNodeP = new Node<T>;
//create pointer, assign pointer to pointer in Node
//Cell<T> *cellPtr = new Cell<T>(count, name, color, index, max_num, price);
//newNode->info= cellPtr;
//creating anonymous object and assigning to the node? I think
newNodeP->info = new Cell<T>(color, index, max_num, name, price);
//weird way I was just experimenting with
newNodeP->info->Cell<T>(count, name, color, index, max_num, price);
//fills node values(this is what I want to handle in the object
newNodeP->color = color;
newNodeP->index = index;
newNodeP->max_num = max_num;
newNodeP->name = name;
newNodeP->nodeNumber += count;
newNodeP->price = price;
newNodeP->linkP = NULL;
if (firstP != NULL)
lastP->linkP = newNodeP;
else
firstP = newNodeP;
lastP = newNodeP;
count++;
}
}
Currently, I have two ways of returning the node landed on. One returns a Node* and sort of works. It returns the pointer to the node, and I can access the values inside that node, but I can't figure out how to store the pointer to that node.
//Find Cell
template<class T>
Node<T>* Board<T>::findCell(int id) {
for (Node<T> *traverseP = firstP; traverseP != NULL; traverseP = traverseP->linkP) {
if (traverseP->nodeNumber == id) {
return traverseP;
}
}
return nullptr;
}
//how I call it in main. it returns an address to that node, but I'm getting errors trying to store that address in a pointer.
cout << "You landed on cell " << gameBoard.findCell(player.getCellNum()) << endl << endl;
Node<T> *ptr = gameboard.findCell(player.getCellNum())->info;
This second way, I think returns the reference to the object in the node, but my earlier problem is stopping me from figuring that out.
//Return Cell
template <class T>
T Board<T>::returnCell(int id) {
for (Node<T> *traverseP = firstP; traverseP != NULL; traverseP = traverseP->linkP) {
if (traverseP->nodeNumber == id) {
return traverseP->info;
}
}
return nullptr;
}
//How i'm calling it in main. I don't really know what it's returning though because it only prints "You landed on " and then nothing else.
cout << "You landed on " << gameBoard.returnCell(player.getCellNum()) << endl;

Getline() and cin manipulate dynamic array

I'm totally lost and confused and could use some help.
I'm currently working on a small command line-based game. For this I wrote a class Inventory, dynamically creating an array of invSpace-objects, each space representing a pair of a pointer to an Item (another class of mine) and a integer, depicting a quantity. Here's the code:
class invSpace {
public:
Item *item;
int quantity;
invSpace() {
item = NULL;
quantity = 0;
}
};
class Inventory {
private:
invSpace* spaces = NULL;
size_t size;
public:
int free_space() {
int free = 0;
for (int i = 0; i < size; i++) {
if (spaces[i].item == NULL) {
free++;
}
}
return free;
}
Inventory() {}
Inventory(size_t new_size) {
size = new_size;
spaces = new invSpace[size];
for (int i = 0; i < size; i++) { //I know this is obsolete because
spaces[i].item = NULL; //of the invSpace constructor, I
spaces[i].quantity = 0; //just did this for testing
}
~Inventory() {
delete[] spaces;
}
invSpace& operator[](int index) {
return spaces[index];
}
};
There are some more methods in this class, like for adding, deleting and searching for items, but those don't matter now. So this is basically just a simple array within one object, dynamically allocating memory in the constructor and with some extra methods. After being created, the array contains zero elements, or Items, so the free_space() method should return the size of the array. But it doesn't. It returns about half of the size.
My first thought was that something went wrong with the allocation. But at a second glance I noticed that the Inventory is totally fine directly after being created; with exactly as many spaces as requested, all of them set to item=NULL/quantity=0. But after a call of getline() at the start of main() that scans user input and saves it to a string for further analyzing, some spaces get filled with random addresses and integers.
Even stranger, with each new call of getline() some spaces are freed, some others filled. As far as my debugging, experimenting and testing goes, none of these addresses belong to any variable in my program, they are just plain random. Also, at no point is there be any interference with the Inventory and the getline() function or the string it returns. In fact, after being created, no part of this object is used anywhere in the code beside the free_space() method. What's even stranger is that spaces in the Inventory class is marked private, so a method is required to meddle with this pointer/array (or so I would expect).
This problem occurs with getline() and cin but not with any of C's <stdio.h> input stream functions. Using malloc() instead of new[] makes no difference. Of course, I could use something like scanf() for the reading from the console. Still, I just want to know why all these things happen. I have absolutely no idea.
Thanks in advance for every answer!
EDIT:
I narrowed the whole code so that it still produces the same error, also changed free_space() so that it prints adress and integer if present:
#include <iostream>
#include <string>
#include <map>
using namespace std;
class Item {
public:
static map<string, Item*> itemlist;
string name;
string description;
Item() {}
Item(const string new_name, const string new_description) {
name = new_name;
description = new_description;
itemlist.insert(pair<string, Item*> (name, this));
}
};
map<string, Item*> Item::itemlist;
/*The more Items are declared, the more random adresses appear in the
inventory*/
Item item01("sword", "A sharp and deadly weapon.");
Item item02("shield", "This will protect you. To a certain extent.");
Item item03("stick", "What is this for exactly?");
Item item04("bottle of water", "A bottle full of refreshing spring water.");
class invSpace {
public:
Item *item;
int quantity;
invSpace() {
item = NULL;
quantity = 0;
}
};
class Inventory {
private:
invSpace* spaces = NULL;
size_t size;
public:
int free_space() {
int free = 0;
for (int i = 0; i < size; i++) {
if (spaces[i].item == NULL) {
free++;
cout << i << " = free" << endl;
}
else {
cout << spaces[i].item << " / " << spaces[i].quantity << endl;
}
}
return free;
}
Inventory() {}
Inventory(size_t new_size) {
size = new_size;
spaces = new invSpace[size];
for (int i = 0; i < size; i++) {
spaces[i].item = NULL;
spaces[i].quantity = 0;
}
}
~Inventory() {
delete[] spaces;
}
};
class Player {
public:
string name;
Inventory inventory;
Player(const string new_name) {
inventory = Inventory(40);
name = new_name;
}
};
Player player("Me");
int main() {
string input;
//Inventory inventory(40); //no error when declared outside the Player class
while (1) {
cout << "\n>> ";
getline(cin, input);
if (input == "x") {
break;
}
else {
player.inventory.free_space();
}
}
}
Some things I noticed: No error occurs if the inventory isn't part of a Player-object. If it is but no Items are declared only the first inventory space receives a random adress (and int value) after the first call of getline().
The more Items there are, the more random adresses I get, it seems...

C++ program crashing (has something to do with with dynamic memory)

I'm having the weird crashing that occurs everytime it goes to the for loop to initialize each position to undefined. Can anyone shine light on why this is happening?
#include
#include
#include
using namespace std;
class MyPhoneBook
{
public:
MyPhoneBook(int, string, string, string, string);
MyPhoneBook();
~MyPhoneBook();
void initialise(int, int, string, string, string, string);
bool search(string&, int);
bool find_free_pos();
void add();
void remove();
void display(int);
friend istream& operator >> (istream& in, MyPhoneBook& ph);
friend ostream& operator << (ostream& out, MyPhoneBook& ph);
private:
int *recordid;
int *status; // -1 is no longer at this number, 0 is blocked, 1 is not blocked, 2 is free
string *name, *areacode, *number, *group;
};
int main(int argc, char** argv)
{
cout << "test 1" << endl;
MyPhoneBook *myphbk; // pointer that will point to an object of a MyPhoneBook class
myphbk = new MyPhoneBook[100]; // now its using dynamic memory
cout << "test 2" << endl; //just for testing
int pos = 0;
for(pos = 0; pos < 100; pos++) // initializing everything to undefined, and position to free (position is the second parameter sended in)
{
myphbk[pos].initialise( (pos+1) , 2 , "Undefined" , "Undefined" , "Undefined" , "Undefined");
}
cout << "test 3" << endl; //just for testing
}
return 0;
void MyPhoneBook::initialise(int recordid_,int status_, string name_, string areacode_, string number_, string group_)
{
//now assign them to private member variables
*recordid = recordid_;
*status = status_;
*name = name_;
*areacode = areacode_;
*number = number_;
*group = group_;
//end of assigning
}
does anyone have any idea why can't my program reach the cout << "test 3" << endl part of the program without crashing?
Since you did not paste the constructor of MyPhoneBook i can only guess, but the problem could be the lines
*recordid = recordid_;
*status = status_;
if you did not assign a valid address to recordid and status in the constructor, e.g. by
recordid = new int;
status = new int;
You might want to declare those member variables as a simple int.

Unhandled exception while reading structures

The program throws an unhandled exception on this line:
}else if(s == "backpack"){
cout << "\nEquipped items: " << endl;
cout << weapon->Name << endl << cArmour->Name << endl; //this line
It prints 'Equipped items: ' then throws an exception. This file - Player.h - includes Library.h, which in turn includes Globals.h, which has the structs:
struct sWeapon{
std::string Name;
int Damage;
};
struct sArmour{
std::string Name;
int AP;
};
In the Player constructor, it creates the struct objects:
Player::Player(std::map<std::string,sWeapon*> wepArray,std::map<std::string,sArmour*> armArray){
weapons = wepArray;
armour = armArray;
weapon = wepArray["None"];
cArmour = armArray["None"];
}
At the beginning of the entire program, it calls init_weapons and init_armour:
int main(){
using namespace std;
//initialise the game
std::map<std::string,sWeapon*> wepArray = init_weapons(); //get weapon array
std::map<std::string,sArmour*>armArray = init_armour(); //get armour array
which return the map of all the weapons:
//init_weapons()
//sets up weapons map
std::map<std::string,sWeapon*> init_weapons(void){
std::map< std::string, sWeapon* > weapons; //map of weapons
//starting 'none'
sWeapon* none = new sWeapon();
none->Name = "None";
none->Damage = 0;
//create weapons
sWeapon* w1 = new sWeapon();
w1->Name = "Rusty dagger";
w1->Damage = 3;
//put in map
weapons[w1->Name] = w1;
return weapons;
}
std::map<std::string,sArmour*> init_armour(void){
std::map< std::string, sArmour* > armour; //map of armour
//starting 'none'
sArmour* none = new sArmour();
none->Name = "None";
none->AP = 0;
//create armour
sArmour* a1 = new sArmour();
a1->Name = "Leather";
a1->AP = 10;
//put in map
armour[a1->Name] = a1;
return armour;
}
then passes those maps as arguments to the player constructor shown above.
I'm guessing either weapon or cArmour is null or point nowhere.
That's all more likely since you're not storing your "None" weapon and armor in your global hash.
Try printing out the pointer for those two "None" objects, then the pointer values for the object members weapon or cArmour.