malloc: *** error for object 0x10ee008c0: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Abort trap: 6
Or I get this when I try and print everything
Segmentation fault: 11
I'm doing some homework for an OOP class and I've been stuck for a good hour now. I'm getting this error once I've used keyboard input enough. I am not someone who gets frustrated at all, and here I am getting very frustrated with this. Here are the files:
This is the book class. I'm pretty sure this is very solid. But just for reference:
//--------------- BOOK.CPP ---------------
// The class definition for Book.
//
#include <iostream>
#include "book.h"
using namespace std;
Book::Book()
//
{
strcpy(title, " ");
strcpy(author, " ");
type = FICTION;
price = 0;
}
void Book::Set(const char* t, const char* a, Genre g, double p)
{
strcpy(title, t);
strcpy(author, a);
type = g;
price = p;
}
const char* Book::GetTitle() const
{
return title;
}
const char* Book::GetAuthor() const
{
return author;
}
double Book::GetPrice() const
{
return price;
}
Genre Book::GetGenre() const
{
return type;
}
void Book::Display() const
{
int i;
cout << GetTitle();
for (i = strlen(title) + 1; i < 32; i++)
cout << (' ');
cout << GetAuthor();
for (i = strlen(author) + 1; i < 22; i++)
cout << (' ');
switch (GetGenre())
{
case FICTION:
cout << "Fiction ";
break;
case MYSTERY:
cout << "Mystery ";
break;
case SCIFI:
cout << "SciFi ";
break;
case COMPUTER:
cout << "Computer ";
break;
}
cout << "$";
if (GetPrice() < 1000)
cout << " ";
if (GetPrice() < 100)
cout << " ";
if (GetPrice() < 10)
cout << " ";
/* printf("%.2f", GetPrice());*/
cout << '\n';
}
This is the store class that deals with the array and dynamic allocation. This was working well without input commands, but just using its functions it was working like a champ.
//--------------- STORE.CPP ---------------
// The class definition for Store.
//
#include <iostream>
#include <cstring> // for strcmp
#include "store.h"
using namespace std;
Store::Store()
{
maxSize = 5;
currentSize = 0;
bookList = new Book[maxSize];
}
Store::~Store()
// This destructor function for class Store
// deallocates the Store's list of Books
{
delete [] bookList;
}
void Store::Insert(const char* t, const char* a, Genre g, double p)
// Insert a new entry into the direrctory.
{
if (currentSize == maxSize)// If the directory is full, grow it.
Grow();
bookList[currentSize++].Set(t, a, g, p);
}
void Store::Sell(const char* t)
// Sell a book from the store.
{
char name[31];
strcpy(name, t);
int thisEntry = FindBook(name);// Locate the name in the directory.
if (thisEntry == -1)
cout << *name << " not found in directory";
else
{
cashRegister = cashRegister + bookList[thisEntry].GetPrice();
// Shift each succeding element "down" one position in the
// Entry array, thereby deleting the desired entry.
for (int j = thisEntry + 1; j < currentSize; j++)
bookList[j - 1] = bookList[j];
currentSize--;// Decrement the current number of entries.
cout << "Entry removed.\n";
if (currentSize < maxSize - 5)// If the directory is too big, shrink it.
Shrink();
}
}
void Store::Find(const char* x) const
// Display the Store's matches for a title or author.
{
// Prompt the user for a name to be looked up
char name[31];
strcpy(name, x);
int thisBook = FindBook(name);
if (thisBook != -1)
bookList[thisBook].Display();
int thisAuthor = FindAuthor(name, true);
if ((thisBook == -1) && (thisAuthor == -1))
cout << name << " not found in current directory\n";
}
void Store::DisplayGenre(const Genre g) const
{
double genrePrice = 0;
int genreCount = 0;
for (int i = 0; i < currentSize; i++)// Look at each entry.
{
if (bookList[i].GetGenre() == g)
{
bookList[i].Display();
genrePrice = genrePrice + bookList[i].GetPrice();
genreCount++;
}
}
cout << "Number of books in this genre: " << genreCount
<< " " << "Total: $";
if (genrePrice < 1000)
cout << " ";
if (genrePrice < 100)
cout << " ";
if (genrePrice < 10)
cout << " ";
printf("%.2f", genrePrice);
}
void Store::DisplayStore() const
{
if (currentSize >= 1)
{
cout << "**Title**\t\t"
<< "**Author**\t"
<< "**Genre**\t"
<< "**Price**\n\n";
for (int i = 0; i < currentSize; i++)
bookList[i].Display();
}
else
cout << "No books currently in inventory\n\n";
cout << "Total Books = " << currentSize
<< "\nMoney in the register = $";
if (cashRegister < 1000)
cout << " ";
if (cashRegister < 100)
cout << " ";
if (cashRegister < 10)
cout << " ";
printf("%.2f", cashRegister);
cout << '\n';
}
void Store::Sort(char type)
{
Book temp;
for(int i = 0; i <= currentSize; i++)
{
for (int j = i+1; j < currentSize; j++)
{
if (type == 'A')
{
if (strcmp(bookList[i].GetTitle(), bookList[j].GetTitle()) > 0)
{
temp = bookList[i];
bookList[i] = bookList[j];
bookList[j] = temp;
}
}
if (type == 'T')
{
if (strcmp(bookList[i].GetAuthor(), bookList[j].GetAuthor()) > 0)
{
temp = bookList[i];
bookList[i] = bookList[j];
bookList[j] = temp;
}
}
}
}
}
void Store::SetCashRegister(double x)
// Set value of cash register
{
cashRegister = x;
}
void Store::Grow()
// Double the size of the Store's bookList
// by creating a new, larger array of books
// and changing the store's pointer to refer to
// this new array.
{
maxSize = currentSize + 5;// Determine a new size.
cout << "** Array being resized to " << maxSize
<< " allocated slots" << '\n';
Book* newList = new Book[maxSize];// Allocate a new array.
for (int i = 0; i < currentSize; i++)// Copy each entry into
newList[i] = bookList[i];// the new array.
delete [] bookList;// Remove the old array
bookList = newList;// Point old name to new array.
}
void Store::Shrink()
// Divide the size of the Store's bookList in
// half by creating a new, smaller array of books
// and changing the store's pointer to refer to
// this new array.
{
maxSize = maxSize - 5;// Determine a new size.
cout << "** Array being resized to " << maxSize
<< " allocated slots" << '\n';
Book* newList = new Book[maxSize];// Allocate a new array.
for (int i = 0; i < currentSize; i++)// Copy each entry into
newList[i] = bookList[i];// the new array.
delete [] bookList;// Remove the old array
bookList = newList;// Point old name to new array.
}
int Store::FindBook(char* name) const
// Locate a name in the directory. Returns the
// position of the entry list as an integer if found.
// and returns -1 if the entry is not found in the directory.
{
for (int i = 0; i < currentSize; i++)// Look at each entry.
if (strcmp(bookList[i].GetTitle(), name) == 0)
return i;// If found, return position and exit.
return -1;// Return -1 if never found.
}
int Store::FindAuthor(char* name, const bool print) const
// Locate a name in the directory. Returns the
// position of the entry list as an integer if found.
// and returns -1 if the entry is not found in the directory.
{
int returnValue;
for (int i = 0; i < currentSize; i++)// Look at each entry.
if (strcmp(bookList[i].GetAuthor(), name) == 0)
{
if (print == true)
bookList[i].Display();
returnValue = i;// If found, return position and exit.
}
else
returnValue = -1;// Return -1 if never found.
return returnValue;
}
Now this is the guy who needs some work. There may be some stuff blank so ignore that. This one controls all the input, which is the problem I believe.
#include <iostream>
#include "store.h"
using namespace std;
void ShowMenu()
// Display the main program menu.
{
cout << "\n\t\t*** BOOKSTORE MENU ***";
cout << "\n\tA \tAdd a Book to Inventory";
cout << "\n\tF \tFind a book from Inventory";
cout << "\n\tS \tSell a book";
cout << "\n\tD \tDisplay the inventory list";
cout << "\n\tG \tGenre summary";
cout << "\n\tO \tSort inventory list";
cout << "\n\tM \tShow this Menu";
cout << "\n\tX \teXit Program";
}
char GetAChar(const char* promptString)
// Prompt the user and get a single character,
// discarding the Return character.
// Used in GetCommand.
{
char response;// the char to be returned
cout << promptString;// Prompt the user
cin >> response;// Get a char,
response = toupper(response);// and convert it to uppercase
cin.get();// Discard newline char from input.
return response;
}
char Legal(char c)
// Determine if a particular character, c, corresponds
// to a legal menu command. Returns 1 if legal, 0 if not.
// Used in GetCommand.
{
return((c == 'A') || (c == 'F') || (c == 'S') ||
(c == 'D') || (c == 'G') || (c == 'O') ||
(c == 'M') || (c == 'X'));
}
char GetCommand()
// Prompts the user for a menu command until a legal
// command character is entered. Return the command character.
// Calls GetAChar, Legal, ShowMenu.
{
char cmd = GetAChar("\n\n>");// Get a command character.
while (!Legal(cmd))// As long as it's not a legal command,
{// display menu and try again.
cout << "\nIllegal command, please try again . . .";
ShowMenu();
cmd = GetAChar("\n\n>");
}
return cmd;
}
void Add(Store s)
{
char aTitle[31];
char aAuthor[21];
Genre aGenre = FICTION;
double aPrice = 10.00;
cout << "Enter title: ";
cin.getline(aTitle, 30);
cout << "Enter author: ";
cin.getline(aAuthor, 20);
/*
cout << aTitle << " " << aAuthor << "\n";
cout << aGenre << " " << aPrice << '\n';
*/
s.Insert(aTitle, aAuthor, aGenre, aPrice);
}
void Find()
{
}
void Sell()
{
}
void ViewGenre(Store s)
{
char c;
Genre result;
do
c = GetAChar("Enter Genre - (F)iction, (M)ystery, (S)ci-Fi, or (C)omputer: ");
while ((c != 'F') && (c != 'M') && (c != 'S') && (c != 'C'));
switch (result)
{
case 'F': s.DisplayGenre(FICTION); break;
case 'M': s.DisplayGenre(MYSTERY); break;
case 'S': s.DisplayGenre(SCIFI); break;
case 'C': s.DisplayGenre(COMPUTER); break;
}
}
void Sort(Store s)
{
char c;
Genre result;
do
c = GetAChar("Enter Genre - (F)iction, (M)ystery, (S)ci-Fi, or (C)omputer: ");
while ((c != 'A') && (c != 'T'));
s.Sort(c);
}
void Intro(Store s)
{
double amount;
cout << "*** Welcome to Bookstore Inventory Manager ***\n"
<< "Please input the starting money in the cash register: ";
/* cin >> amount;
s.SetCashRegister(amount);*/
}
int main()
{
Store mainStore;// Create and initialize a Store.
Intro(mainStore);//Display intro & set Cash Regsiter
ShowMenu();// Display the menu.
/*mainStore.Insert("A Clockwork Orange", "Anthony Burgess", SCIFI, 30.25);
mainStore.Insert("X-Factor", "Anthony Burgess", SCIFI, 30.25);*/
char command;// menu command entered by user
do
{
command = GetCommand();// Retrieve a command.
switch (command)
{
case 'A': Add(mainStore); break;
case 'F': Find(); break;
case 'S': Sell(); break;
case 'D': mainStore.DisplayStore(); break;
case 'G': ViewGenre(mainStore); break;
case 'O': Sort(mainStore); break;
case 'M': ShowMenu(); break;
case 'X': break;
}
} while ((command != 'X'));
return 0;
}
Please, any and all help you can offer is amazing.
Thank you.
Welcome to the exciting world of C++!
Short answer: you're passing Store as a value. All your menu functions should take a Store& or Store* instead.
When you're passing Store as a value then an implicit copy is done (so the mainStore variable is never actually modified). When you return from the function the Store::~Store is called to clean up the copied data. This frees mainStore.bookList without changing the actual pointer value.
Further menu manipulation will corrupt memory and do many double frees.
HINT: If you're on linux you can run your program under valgrind and it will point out most simple memory errors.
Your Store contains dynamically-allocated data, but does not have an assignment operator. You have violated the Rule of Three.
I don't see the Store class being instantiated anywhere by a call to new Store() which means the booklist array has not been created but when the program exits and calls the destructor, it tries to remove the array that was never allocated and hence that's why i think you are getting this error. Either, modify the destructor to have a null check or instantiate the class by a call to the constructor. Your code shouldn't still be working anywhere you are trying to use a Store object.
Hope this helps
Related
void ChoreStack::add(int new_urgency_level, string new_job){
for(int i = 0; i < length_; i++){
Chore* temp_chore = chore_array_[i];
if(temp_chore->level == new_urgency_level){
temp_chore->joblist.append(new_job+"^");
}
chore_array_[i] = temp_chore;
free(temp_chore);
}
}
Hello, this is the part that Valgrind says "Invalid read of size 4". I have never learned anything related to memory allocation before. Could anyone please explain to me in details why there is a memory error and how to fix it? Thank you!
Here is my declaration:
class ChoreStack{
public:
ChoreStack();
~ChoreStack();
void initialize(int new_urgency_level);
void add(int new_urgency_level, string new_chore);
void erase(int erase_level);
void print();
void next();
int get_length();
private:
struct Chore{
int level;
string joblist;
};
Chore** chore_array_;
int length_;
string* split_joblist(string joblist);
int number_of_chores(string joblist);
};
ChoreStack::ChoreStack(): chore_array_(nullptr), length_(0){
}
ChoreStack::~ChoreStack(){
delete[] chore_array_;
}
void ChoreStack::initialize(int new_urgency_level){
delete[] chore_array_;
chore_array_ = new Chore*[new_urgency_level];
for(int i = 0; i < new_urgency_level; i++){
Chore* temp_chore = new Chore;
temp_chore->level = i+1;
temp_chore->joblist = "";
chore_array_[new_urgency_level-i-1] = temp_chore;
delete temp_chore;
}
length_ = new_urgency_level;
cout << "New priority list with levels 1-" << length_ << " initialized." << endl;
}
Here is the part related to ChoreStack::add() function in main():
int main(){
ChoreStack c;
string cmd_line;
string* cmd_ptr = new string[3];
bool initialized = false;
while(true){
cout << "chores> ";
getline(cin, cmd_line);
int cmd_num = 0;
if(cmd_line.find_first_not_of("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890 ") != string::npos){
cout << "Error: invalid input." << endl;
} else {
int begin_i = 0, space_occurence = 0;
unsigned int i = 0;
while(i <= cmd_line.length()){
if(cmd_line[i] == ' ' || i == cmd_line.length()){
space_occurence++;
cmd_num++;
if(space_occurence == 3){
cmd_ptr[cmd_num-1] = cmd_line.substr(begin_i);
break;
} else {
cmd_ptr[cmd_num-1] = cmd_line.substr(begin_i, i - begin_i);
}
begin_i = i + 1;
}
i++;
}
string command = cmd_ptr[0];
if(command == "init"){
if(cmd_num == 1){
cout << "Error: the number of priority levels is missing." << endl;
} else if(cmd_num > 2){
cout << "Error: too much input for initializing." << endl;
} else {
if(cmd_ptr[1].find_first_not_of("1234567890") != string::npos){
cout << "Error: the number of priority levels must be an integer larger than 0." << endl;
} else if(stoi(cmd_ptr[1]) < 1){
cout << "Error: it is not possible to create a priority list with 0 or less levels." << endl;
} else {
c.initialize(stoi(cmd_ptr[1]));
initialized = true;
}
}
} else if(command == "add"){
if(!initialized){
cout << "Error: no priority list is initialized." << endl;
} else {
if(cmd_num == 1){
cout << "Error: priority level and chore description are missing." << endl;
} else if(cmd_ptr[1].find_first_not_of("1234567890") != string::npos
|| stoi(cmd_ptr[1]) < 1
|| stoi(cmd_ptr[1]) > c.get_length()){
cout << "Error: priority level must be an integer between 1-3." << endl;
} else if(cmd_num == 2){
cout << "Error: chore description is missing." << endl;
} else {
c.add(stoi(cmd_ptr[1]), cmd_ptr[2]);
}
}
}
Here is the error message:
invalid read of size 4:
1.ChoreStack::add(int,std::__cxx11::basic_string<char,std::char_traits<char>,std::allocator<char>>)
2.main
Address 0x5a9de70 is 0 bytes inside a block of size 40 free'd
1. operator delete(void*)
2. ChoreStack::initialize(int)
3. main
Block was alloc'd at
1. operator new(unsigned long)
2. ChoreStack::initialize(int)
3. main
and there are lot more errors in the same form of this one...
This is a classic access after free.
void ChoreStack::initialize(int new_urgency_level){
delete[] chore_array_;
chore_array_ = new Chore*[new_urgency_level];
for(int i = 0; i < new_urgency_level; i++){
Chore* temp_chore = new Chore;
temp_chore->level = i+1;
temp_chore->joblist = "";
chore_array_[new_urgency_level-i-1] = temp_chore;
delete temp_chore;
}
Look closely at this code. You create a pointer called temp_chore. It points to an object you allocate with new. You then copy the value of temp_chore into chore_array_. So now the array has a pointer to the object you allocated with new.
But then you delete the object you allocated. So now, chore_array_ has a pointer to an object you deleted. It is an error to attempt to dereference this pointer since the thing it points to no longer exists.
But then in add, we have:
Chore* temp_chore = chore_array_[i];
if(temp_chore->level == new_urgency_level){
So temp_chore->level is an attempt to access the level member of the object temp_chore points to. But you deleted that object.
// Once you do this,
chore_array_[new_urgency_level-i-1] = temp_chore;
// this
delete temp_chore;
// does the same thing as this
delete chore_array_[new_urgency_level-i-1];
// because they're equal
You will find things much easier if you keep collections of values rather than collections of raw pointers.
Novice C++ user trying to make a program that uses arrays to store names. The output is a menu that you enter a command and it does said function. The problem is that when I attempt to enter a command from the menu, the program crashes. I don't get any errors or warnings when compiling so I'm at a loss for why it crashes.
Any feedback / help is greatly appreciated
#include <iostream>
#include <string>
using namespace std;
//Prototypes----------------------------------------------------------------------------------------------------------------------------------------------------------------
void addName (string nameList[], const int listMax); //Adds a name
void removeName (string nameList[], const int listMax); //Removes a name
void findName (string nameList[], const int listMax); // Searches for a name in the saved list
void showList (string nameList[], const int listMax); //Shows a list of names currentllistMax stored
void sortList (string nameList[], const int listMax); //Sorts saved names in alphabetical order
//Global Declartions--------------------------------------------------------------------------------------------------------------------------------------------------------
const int listMax = 50;
// MAIN FUNCTION -------------------------------------------------------------------------------------------------------------------------------------------------------------
int main(){
char input; // Variable for when the user inputs their menu selection
string nameList[listMax]; //string called names with the size being namesMax which is 50
cout << "Enter a Command " << endl; //Prompt to choose command from menu
cout << "<A> - Add a name to the database(Max 10)." << endl; //getNames
cout << "<R> - Remove a name from the database" << endl; //removeName
cout << "<F> - Search for a name in the database" << endl; //findName
cout << "<L> - Shows current state of list" << endl; //displalistMaxNames
cout << "<S> - Sorts the database." << endl; //sortNames
cout << "<Q> - Ends the program." << endl; //Exits the program entirellistMax
cin >> input; //Saves the menu choice input
if (input == 'A' ||input == 'a')
{
addName(nameList, listMax);
}
else if (input == 'R' || input == 'r')
{
removeName(nameList, listMax);
}
else if (input == 'F' || input == 'f')
{
findName(nameList, listMax);
}
else if (input == 'L' || input == 'l')
{
showList(nameList, listMax);
}
else if (input == 'S' || input == 's')
{
sortList(nameList, listMax);
}
else if (input == 'Q' || input == 'q')
{
return 0;
}
}
// ADD NAMES FUNCTION --------------------------------------------------------------------------------------------------------------------------------------------------------
void addName(string nameList[], const int listMax){
int x;
for (x = 0; x < listMax; x++) //x = 0 > if x is less than listMax, increment blistMax 1
{
cout << "Enter a name to be added to the database: ";
cin >> nameList[listMax];
}
return;
}
// REMOVE NAMES FUNCTION -----------------------------------------------------------------------------------------------------------------------------------------------------
void removeName(string nameList[], const int listMax){
string clearName; //EmptlistMax String
string removeName;
cout << "Enter the name to be removed from the database: ";
cin >> removeName; //The name entered saved to removeName for further computation
for (int x = 0; x < listMax; x++) // x = 0, if x is less than listMax; increment blistMax 1
{
if(nameList[x] == removeName) // Goes through everlistMax arralistMax level searching to see if it compares to the name saved to removeName
{
nameList[x] = clearName; // If name is found, assign value of clearnName, which is an emptlistMax string, thus removing it from the arralistMax.
}
else if(x == listMax - 1) // If it goes through the entire arralistMax and doesnt find a match it outputs that the name could not be found.
{
cout << "Name not found";
}
}
return;
}
// SEARCH FUNCTION -----------------------------------------------------------------------------------------------------------------------------------------------------------
void findName(string nameList[], const int listMax){
int x = 0;
int z;
bool f = false;
string searchName;
cout << "Enter a name to search for";
cin >> searchName;
while (!f && x <= listMax) //While f and x are equal to / less than listMax
{
int listMax = (x + z) / 2;
if (nameList[listMax] == searchName) // if our arralistMax is on level listMax then its on the list
{
f = true;
cout << nameList[listMax] << "is on the list!" <<endl;
}
else if (nameList[listMax] > searchName)
z = listMax - 1;
else
x = listMax + 1;
}
return;
}
// DISPLAY FUNCTION ----------------------------------------------------------------------------------------------------------------------------------------------------------
void showList(string nameList[], const int listMax){
cout << nameList[listMax];
return;
}
// SORT NAMES FUNCTION -------------------------------------------------------------------------------------------------------------------------------------------------------
void sortList(string nameList[], const int listMax){
bool sort;
do
{
sort = 0;
for (int x = 0; x < listMax - 1; x++)
{
if (nameList[x] > nameList[x + 1])
{
nameList[x].swap(nameList[x+1]);
sort = 1;
}
}
}
while (sort == 1);
return;
}
VisualStudio tells me the code crashes for accessing not-allocated memory.
You use list[listMax] several times, which shouldn't be and isn't OK.
The last item in an array a with length len is a[len-1], not a[len] (Because array indices start from 0 in C).
By the way, it also tells me that in findName() you use z while it's uninitialized, which is undefined behavior.
Iam trying to create an open addressed hash table. The user has to be able to define the table's size. For my collision resolution I am using psuedo random probing -
http://algoviz.org/OpenDSA/Books/OpenDSA/html/HashCImproved.html#HashingPseudoRandomProbePRO
Anyway the hashing function(s) seem to work fine until I only have 1 more space to fill in the table. As soon as I input the final element of the table my program crashes and I get a segmentation fault. I've been pulling my hair out trying to find the problem for hours. Please help!
#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
using namespace std;
class HashTable
{
string* hash;
bool* inTable;
vector<int> perm;
int table_size;
int permFunc(int, int);
void permutation();
int hashFunc(string);
public:
//constructor for a hashtable
HashTable(int);
//deconstructor for hashtable
~HashTable();
bool isFull();
//The prototype for the insert method
void insert(string);
//The prototype for the lookup method
bool lookup(string);
//The prototype for the remove method
void remove(string);
//The prototype for the size method
int size();
//The prototype for the print method
void print();
};
//constructor
HashTable::HashTable(int table_size)
{
this -> table_size = table_size;
hash = new string[table_size];
inTable = new bool[table_size];
for(int i=0; i<table_size; i++) perm.push_back(i);
permutation();
}
//deconstructor
HashTable::~HashTable()
{
delete [] hash;
delete [] inTable;
}
//creates a permutation of the perm vectors index elements
void HashTable::permutation()
{
random_shuffle ( perm.begin() + 1, perm.end() );
}
//Hash function method for strings
int HashTable::hashFunc(string str)
{
int key = 0;
for (int i=0; i < str.size(); i++)
{
key += static_cast<int>(str.at(i));
}
cout << "key is:" << key << endl;
return key%table_size;
}
int HashTable::permFunc(int index, int i)
{
int temp = index;
index += perm[i];
if(index > table_size){index = index - table_size;}
if(inTable[index] == false ){return index;}
index = temp;
i++;
permFunc(index, i);
}
bool HashTable::isFull()
{
int num=0;
for(int i=0;i<table_size; i++)
{
if(inTable[i] == true){num ++;}
if(num == table_size){
cout<<"FULL TABLE";
return true;}
}
return false;
}
//Method that inserts an integer into the hash table
void HashTable::insert(string str)
{
int i=1;
if(isFull()){return;}
int pos = hashFunc(str);
if(inTable[pos] == true)
{
int permPos = permFunc(pos, i);
hash[permPos] = str;
inTable[permPos] = true;
}
else
hash[pos] = str;
inTable[pos] = true;
}
//A function that prints out the menu
void menu()
{
cout << endl;
cout << "press i to insert an element into the hash table" << endl;
cout << "press d to inTableete an element from the hash table" << endl;
cout << "press l to look up an element" << endl;
cout << "press s to obtain the size of the table" << endl;
cout << "press p to print the current table" << endl;
return;
}
//The main method
//Implements all the input and output
int main(void)
{
int table_size;
cout << "Enter table size: ";
cin >> table_size;
HashTable h(table_size);
while(true)
{
char c;
string str;
menu();
getline(cin, str);
stringstream stream;
stream << str;
stream >> c;
switch(c)
{
case 'i':
getline(cin, str);
h.insert(str);
break;
case 'd':
getline(cin, str);
h.remove(str);
break;
case 'l':
getline(cin, str);
cout << h.lookup(str) << "\n";
break;
case 's':
cout << h.size() << "\n";
break;
case 'p':
h.print();
break;
default:
cout << "Don't understand '" << c << "'\n";
}
}
return 0;
}
I don't know if this is your problem but this line:
if(index > table_size){index = index - table_size;}
will leave index at table_size which is out of bounds for your array. Change the > to >=
If this isn't it, could you add an example crashing input to your post, for testing?
I'm very new to programming and I am trying to write a program that adds and subtracts polynomials. My program sometimes works, but most of the time, it randomly crashes and I have no idea why. It's very buggy and has other problems I'm trying to fix, but I am unable to really get any further coding done since it crashes. I'm completely new here but any help would be greatly appreciated.
Here's the code:
#include <iostream>
#include <cstdlib>
using namespace std;
int getChoice();
class Polynomial10
{
private:
double* coef;
int degreePoly;
public:
Polynomial10(int max); //Constructor for a new Polynomial10
int getDegree(){return degreePoly;};
void print(); //Print the polynomial in standard form
void read(); //Read a polynomial from the user
void add(const Polynomial10& pol); //Add a polynomial
void multc(double factor); //Multiply the poly by scalar
void subtract(const Polynomial10& pol); //Subtract polynom
};
void Polynomial10::read()
{
cout << "Enter degree of a polynom between 1 and 10 : ";
cin >> degreePoly;
cout << "Enter space separated coefficients starting from highest degree" << endl;
for (int i = 0; i <= degreePoly; i++) cin >> coef[i];
}
void Polynomial10::print()
{
for (int i = 0;i <= degreePoly; i++) {
if (coef[i] == 0) cout << "";
else if (i >= 0) {
if (coef[i] > 0 && i != 0) cout<<"+";
if ((coef[i] != 1 && coef[i] != -1) || i == degreePoly) cout << coef[i];
if ((coef[i] != 1 && coef[i] != -1) && i != degreePoly ) cout << "*";
if (i != degreePoly && coef[i] == -1) cout << "-";
if (i != degreePoly) cout << "x";
if ((degreePoly - i) != 1 && i != degreePoly) {
cout << "^";
cout << degreePoly-i;
}
}
}
}
void Polynomial10::add(const Polynomial10& pol)
{
for(int i = 0; i<degreePoly; i++) {
int degree = degreePoly;
coef[degreePoly-i] += pol.coef[degreePoly-(i+1)];
}
}
void Polynomial10::subtract(const Polynomial10& pol)
{
for(int i = 0; i<degreePoly; i++) {
coef[degreePoly-i] -= pol.coef[degreePoly-(i+1)];
}
}
void Polynomial10::multc(double factor)
{
//int degreePoly=0;
//double coef[degreePoly];
cout << "Enter the scalar multiplier : ";
cin >> factor;
for(int i = 0; i<degreePoly; i++) coef[i] *= factor;
}
Polynomial10::Polynomial10(int max)
{
degreePoly = max;
coef = new double[degreePoly];
for(int i; i < degreePoly; i++) coef[i] = 0;
}
int main()
{
int choice;
Polynomial10 p1(1),p2(1);
cout << endl << "CGS 2421: The Polynomial10 Class" << endl << endl << endl;
cout
<< "0. Quit\n"
<< "1. Enter polynomial\n"
<< "2. Print polynomial\n"
<< "3. Add another polynomial\n"
<< "4. Subtract another polynomial\n"
<< "5. Multiply by scalar\n\n";
int choiceFirst = getChoice();
if (choiceFirst != 1) {
cout << "Enter a Polynomial first!";
}
if (choiceFirst == 1) {choiceFirst = choice;}
while(choice != 0) {
switch(choice) {
case 0:
return 0;
case 1:
p1.read();
break;
case 2:
p1.print();
break;
case 3:
p2.read();
p1.add(p2);
cout << "Updated Polynomial: ";
p1.print();
break;
case 4:
p2.read();
p1.subtract(p2);
cout << "Updated Polynomial: ";
p1.print();
break;
case 5:
p1.multc(10);
cout << "Updated Polynomial: ";
p1.print();
break;
}
choice = getChoice();
}
return 0;
}
int getChoice()
{
int c;
cout << "\nEnter your choice : ";
cin >> c;
return c;
}
When you create your objects with p1(1) and p2(1) the coef array in each object is allocated to contain one element. Then in read() you simply set degreePoly to a (possibly higher) value but don't change the allocation of coef. It will still contain only one element, but all coefficients are written to it, probably writing over the bounds of the array. Instead the old coefshould be freed and a new array of suitable size be allocated.
Also in add and subtract you are assigning to coefficients out of bounds (for i=0):
coef[degreePoly-i] -= pol.coef[degreePoly-(i+1)];
It also seems wrong mathematically to subtract the coefficients at index degreePoly-(i+1) from those at index degreePoly-i. Additionally the case that the two polygons might have different degrees is currently not handled.
One issue is the coef allocation. You're allocating an array of degreePoly=max elements (1 for the two actual objects). But I think it should be max + 1, since a n-degree polynomial has n + 1 coefficients. This off-by-one error appears elsewhere (e.g. add and mult). Also in the constructor, you are not initializing i, so it has an undefined value, which alone means you can (probably will) write to memory you don't control.
Later, in your read code, you overwrite degreePoly, but do not reallocate coef, so it could be the wrong size, at least if you exceed your maximum. Also note that you are accepting degreePoly + 1 coefficients (which I think is correct) here. This means a max of 1 in the constructor only corresponds to a degreePoly of 0 in read! Entering 1 in read will cause two values to be read, which will overflow the array.
Writing to invalid pointer locations and using uninitialized values causes undefined behavior, which is why it crashes sometimes.
if (choiceFirst == 1) {choiceFirst = choice;}
doesn't make sense since choice is not initialized at this stage. You probably want:
if (choiceFirst == 1) {choice = choiceFirst;}
When I input locations from a txt file I am getting a peculiar error where it seems to miss off the first entry, yet add a garbage entry to the end of the link list (it is designed to take the name, latitude and longitude for each location you will notice). I imagine this to be an issue with where it starts collecting the inputs and where it stops but I cant find the error!! It reads the first line correctly but then skips to the next before adding it because during testing for the bug it had no record of the first location Lisbon though whilst stepping into the method call it was reading it. Very bizarre but hopefully someone knows the issue. Here is firstly my header file:
#include <string>
struct locationNode
{
char nodeCityName [35];
double nodeLati;
double nodeLongi;
locationNode* Next;
void CorrectCase() // Correct upper and lower case letters of input
{
int MAX_SIZE = 35;
int firstLetVal = this->nodeCityName[0], letVal;
int n = 1; // variable for name index from second letter onwards
if((this->nodeCityName[0] >90) && (this->nodeCityName[0] < 123)) // First letter is lower case
{
firstLetVal = firstLetVal - 32; // Capitalise first letter
this->nodeCityName[0] = firstLetVal;
}
while(n <= MAX_SIZE - 1)
{
if((this->nodeCityName[n] >= 65) && (this->nodeCityName[n] <= 90))
{
letVal = this->nodeCityName[n] + 32;
this->nodeCityName[n] = letVal;
}
n++;
}
//cityNameInput = this->nodeCityName;
}
};
class Locations
{
private:
int size;
public:
Locations(){
}; // constructor for the class
locationNode* Head;
//int Add(locationNode* Item);
};
And here is the file containing main:
// U08221.cpp : main project file.
#include "stdafx.h"
#include "Locations.h"
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int n = 0,x, locationCount = 0, MAX_SIZE = 35;
string cityNameInput;
char targetCity[35];
bool acceptedInput = false, userInputReq = true, match = false, nodeExists = false;// note: addLocation(), set to true to enable user input as opposed to txt file
locationNode *start_ptr = NULL; // pointer to first entry in the list
locationNode *temp, *temp2; // Part is a pointer to a new locationNode we can assign changing value followed by a call to Add
locationNode *seek, *bridge;
void setElementsNull(char cityParam[])
{
int y=0, count =0;
while(cityParam[y] != NULL)
{
y++;
}
while(y < MAX_SIZE)
{
cityParam[y] = NULL;
y++;
}
}
void addLocation()
{
temp = new locationNode; // declare the space for a pointer item and assign a temporary pointer to it
if(!userInputReq) // bool that determines whether user input is required in adding the node to the list
{
cout << endl << "Enter the name of the location: ";
cin >> temp->nodeCityName;
temp->CorrectCase();
setElementsNull(temp->nodeCityName);
cout << endl << "Please enter the latitude value for this location: ";
cin >> temp->nodeLati;
cout << endl << "Please enter the longitude value for this location: ";
cin >> temp->nodeLongi;
cout << endl;
}
temp->Next = NULL; //set to NULL as when one is added it is currently the last in the list and so can not point to the next
if(start_ptr == NULL){ // if list is currently empty, start_ptr will point to this node
start_ptr = temp;
}
else
{ temp2 = start_ptr;
// We know this is not NULL - list not empty!
while (temp2->Next != NULL)
{
temp2 = temp2->Next; // Move to next link in chain until reach end of list
}
temp2->Next = temp;
}
++locationCount; // increment counter for number of records in list
if(!userInputReq){
cout << "Location sucessfully added to the database! There are " << locationCount << " location(s) stored" << endl;
}
}
void populateList(){
ifstream inputFile;
inputFile.open ("locations.txt", ios::in);
userInputReq = true;
temp = new locationNode; // declare the space for a pointer item and assign a temporary pointer to it
do
{
inputFile.get(temp->nodeCityName, 35, ' ');
setElementsNull(temp->nodeCityName);
inputFile >> temp->nodeLati;
inputFile >> temp->nodeLongi;
setElementsNull(temp->nodeCityName);
if(temp->nodeCityName[0] == 10) //remove linefeed from input
{
for(int i = 0; temp->nodeCityName[i] != NULL; i++)
{
temp->nodeCityName[i] = temp->nodeCityName[i + 1];
}
}
addLocation();
}
while(!inputFile.eof());
userInputReq = false;
cout << "Successful!" << endl << "List contains: " << locationCount << " entries" << endl;
cout << endl;
inputFile.close();
}
bool nodeExistTest(char targetCity[]) // see if entry is present in the database
{
match = false;
seek = start_ptr;
int letters = 0, letters2 = 0, x = 0, y = 0;
while(targetCity[y] != NULL)
{
letters2++;
y++;
}
while(x <= locationCount) // locationCount is number of entries currently in list
{
y=0, letters = 0;
while(seek->nodeCityName[y] != NULL) // count letters in the current name
{
letters++;
y++;
}
if(letters == letters2) // same amount of letters in the name
{
y = 0;
while(y <= letters) // compare each letter against one another
{
if(targetCity[y] == seek->nodeCityName[y])
{
match = true;
y++;
}
else
{
match = false;
y = letters + 1; // no match, terminate comparison
}
}
}
if(match)
{
x = locationCount + 1; //found match so terminate loop
}
else{
if(seek->Next != NULL)
{
bridge = seek;
seek = seek->Next;
x++;
}
else
{
x = locationCount + 1; // end of list so terminate loop
}
}
}
return match;
}
void deleteRecord() // complete this
{
int junction = 0;
locationNode *place;
cout << "Enter the name of the city you wish to remove" << endl;
cin >> targetCity;
setElementsNull(targetCity);
if(nodeExistTest(targetCity)) //if this node does exist
{
if(seek == start_ptr) // if it is the first in the list
{
junction = 1;
}
if(seek != start_ptr && seek->Next == NULL) // if it is last in the list
{
junction = 2;
}
switch(junction) // will alter list accordingly dependant on where the searched for link is
{
case 1:
start_ptr = start_ptr->Next;
delete seek;
--locationCount;
break;
case 2:
place = seek;
seek = bridge;
delete place;
--locationCount;
break;
default:
bridge->Next = seek->Next;
delete seek;
--locationCount;
break;
}
}
else
{ cout << targetCity << "That entry does not currently exist" << endl << endl << endl;
}
}
void searchDatabase()
{
char choice;
cout << "Enter search term..." << endl;
cin >> targetCity;
if(nodeExistTest(targetCity))
{
cout << "Entry: " << endl << endl;
}
else
{
cout << "Sorry, that city is not currently present in the list." << endl << "Would you like to add this city now Y/N?" << endl;
cin >> choice;
/*while(choice != ('Y' || 'N'))
{
cout << "Please enter a valid choice..." << endl;
cin >> choice;
}*/
switch(choice)
{
case 'Y':
addLocation();
break;
case 'N':
break;
default :
cout << "Invalid choice" << endl;
break;
}
}
}
void printDatabase()
{
temp = start_ptr; // set temp to the start of the list
do
{ if (temp == NULL)
{
cout << "You have reached the end of the database" << endl;
}
else
{ // Display details for what temp points to at that stage
cout << "Location : " << temp->nodeCityName << endl;
cout << "Latitude : " << temp->nodeLati << endl;
cout << "Longitude : " << temp->nodeLongi << endl;
cout << endl;
// Move on to next locationNode if one exists
temp = temp->Next;
}
}
while (temp != NULL);
}
void nameValidation(string name)
{
n = 0; // start from first letter
x = name.size();
while(!acceptedInput)
{
if((name[n] >= 65) && (name[n] <= 122)) // is in the range of letters
{
while(n <= x - 1)
{
while((name[n] >=91) && (name[n] <=97)) // ERROR!!
{
cout << "Please enter a valid city name" << endl;
cin >> name;
}
n++;
}
}
else {
cout << "Please enter a valid city name" << endl;
cin >> name;
}
if(n <= x - 1)
{
acceptedInput = true;
}
}
cityNameInput = name;
}
int main(array<System::String ^> ^args)
{
//main contains test calls to functions at present
cout << "Populating list...";
populateList();
printDatabase();
deleteRecord();
printDatabase();
cin >> cityNameInput;
}
The text file contains this (ignore the names, they are just for testing!!):
Lisbon 45 47
Fattah 45 47
Darius 42 49
Peter 45 27
Sarah 85 97
Michelle 45 47
John 25 67
Colin 35 87
Shiron 40 57
George 34 45
Sean 22 33
The output omits Lisbon, but adds on a garbage entry with nonsense values. Any ideas why? Thank you in advance.
The main function creates a new locationNode and stores it in the global variable temp, then reads the first dataset and stores it in that node.
Then you call addLocation() which starts by creating another new locationNode which replaces the existing one in temp. This new node is then inserted into the list.
The next iteration of the main loop then fills that temp value with values and addLocation() inserts another brand new locationNode into the list. So the first dataset isn't stored in the list and each iteration ends up by inserting an uninitialized new node.
As you see using global variables can lead to confusing situations and you code would surely become clearer if you were passing the nodes as parameters to your functions.