Getting write access violation when trying to re-allocate memory - c++

I am writing a program that takes some contact information from user and grows the array dynamically when it gets full. But when I am trying to run the program I get Write access violation popping up a line from "iosfwd standard header". I don't know where I went wrong. Please do help.
My code looks like this:
# include "pch.h"
# include <iostream>
# include <string>
using namespace std;
struct Contact {
string name;
string number;
string address;
string exit;
};
void userPrompt(Contact &contact) {
cout << "Name: ";
getline(cin, contact.name);
cout << "Phone number: ";
getline(cin, contact.number);
cout << "Address: ";
getline(cin, contact.address);
cout << "Exit? (y/n): ";
getline(cin, contact.exit);
}
void printContact(Contact &contact) {
cout << "Name: " << contact.name << endl;
cout << "Phone number: " << contact.number << endl;
cout << "Address: " << contact.address << "\n" << endl;
}
void growArray(int &currentLength, Contact *contacts) {
int multiplyer = 2;
Contact *new_array = new Contact[currentLength * multiplyer];
for (int i = 0; i < currentLength; i++) {
new_array[i] = contacts[i];
}
delete[] contacts;
contacts = new_array;
currentLength *= multiplyer;
}
void showAllContacts(Contact *contacts, int length) {
for (int i = 0; i < length; i++) {
if (contacts[i].name.length() != 0) {
printContact(contacts[i]);
}
}
}
int main() {
// Prompt the user to fill in the address book.
// If the array gets full, make it bigger.
Contact *contacts = new Contact[1];
int currentLength = 1;
int i = 0;
while (true) {
userPrompt(contacts[i]);
if (contacts[i].exit == "y" or contacts[i].exit == "Y") {
break;
}
i++;
if (i == currentLength) {
growArray(currentLength, contacts);
}
}
// Show the address book
showAllContacts(contacts, currentLength);
}
But when I am running the code it throws exception like this:
enter image description here
"Write Access Violation"
I think the bug is in the growArray function. But I can't fugure out where did I screw up. Please do help.

In
growArray(currentLength, contacts);
a copy of the pointer contacts is modified inside the function; but outside, the pointer's value stays the same. After growArray returns, contacts points to deleted memory, hence UB, hence the crash.
==> Full program demonstration of the issue <==
There are basically two solutions: the bad one and the good one. The bad one is to change the signature of growArray to take a reference to the pointer:
void growArray(int &currentLength, Contact *&contacts)
The good one is to stop this manually allocated memory non-sense and use a std::vector<Contact>!

Related

Issue With Freeing Dynamically Allocated Memory In C++

Before my program can free up memory and end it crashes. Crashes seem to happen on transition from the function UserDataCollection and back to main. This is only my second program using pointers so I'm still quite the newbie considering the whole point of c++ is to use pointers.
Here is the aforementioned code:
#include <iostream>
//Prototypes
void UserDataCollection(int * &movieData_ptr, int &numSurveyed); // Movie Statistics
void DisplayOutput(int *movieData_ptr, int numSurveyed); //Mean, Median, Mode (Display To Console)
//Global Constants
int main()
{
//Variables
int numSurveyed = 0;
//Pointers
int * movieData_ptr = nullptr;
movieData_ptr = new int[numSurveyed];
//"Program Start"
std::cout << "Program start...\n\n";
UserDataCollection(movieData_ptr, numSurveyed);
DisplayOutput(movieData_ptr, numSurveyed);
//Release Memory
delete[] movieData_ptr;
std::cout << "Memory Cleared.";
return 0;
}
void UserDataCollection(int * &movieData_ptr, int &numSurveyed)
{
//Get Number of Students Surveyed
std::cout << "How many students were surveyed: ";
std::cin >> numSurveyed;
//Student Data Input Loop
for (int i = 0; i < numSurveyed; i++)
{
//Get Student Data
std::cout << "Enter How many movies student " << i + 1 << " has seen in ONE month: ";
std::cin >> *(movieData_ptr + i);
//Validation Check
while (*(movieData_ptr + i) >= 337)
{
std::cout << "\nImpossible value!" << std::endl
<< "Hours in a month: 730. Average movie length: 130 minutes."
<< "Total Possible movies: 337";
std::cout << "\n\nEnter How many movies student " << i + 1 << " has seen in ONE month: ";
std::cin >> *(movieData_ptr + i);
} //end while (Validation Check)
} // end for (Data Input)
}
void DisplayOutput(int *movieData_ptr, int numSurveyed)
{
//Display loop for pointer array
for (int i = 0; i < numSurveyed; i++)
{
std::cout << *(movieData_ptr + i) << " ";
}
//End Message
std::cout << "\n\nProgram end.";
}
You never allocated any memory.
int numSurveyed = 0;
//Pointers
int * movieData_ptr = nullptr;
movieData_ptr = new int[numSurveyed];
This is the equivalent of
int *movieData_ptr = new int[0];
You are allocating size of 0 ints. This is undefined behaviour. You can't do anything useful with that pointer without a segmentation fault. You need to either pre-allocate a certain amount, and make sure you don't overflow, or dynamically allocate every time you plan to add data.
Since this is C++, it's probably better not to use raw pointers, but use vector or something instead.
Sorry:
From 5.3.4/7
When the value of the expression in a direct-new-declarator is zero, the allocation function is called to allocate an array with no elements.
From 3.7.3.1/2
The effect of dereferencing a pointer returned as a request for zero size is undefined.

Reading into an Array Multiple Times

I'm having a little trouble with my code. It's pretty much supposed to open two files, and compare the first twenty line of the file "StudentAnswers.txt" [inputted as a char into a char array] against a char value in (each line of another file) "CorrectAnswers.txt" in another array at the same position (index). It's like a linear search, but the same position in the arrays. Then a report should be displayed, detailing which question the student missed, the given answer, the correct answer, and if the student passed (got >= 70%) or not, like the following:
Report for Student X:
2 (A/D), 3 (C/D), 5(D/A)
This student passed the exam!
Then it should clear the SAArray, and feed the next twenty lines from StudentAnswers.txt, and start the process all over again. I guess the program has to determine the number of students from (lines of 'StudentAnswers.txt' file / 20).
I'm having trouble displaying the report, and having the array clear itself after the program. I'm guessing this can be done with a while loop and an accumulator for the number of students (to be determined by above equation).
Also, Visual Studio seems to go to "Missed __ questions for a total of ___ %", and then keep looping -858993460.
Any help would be appreciated.
#include <iostream>
#include <fstream>
#include <string>
#include <array>
#include <algorithm>
using namespace std;
void GradeReturn(char[], char[], int, int, int);
string PassFail(float);
int main()
{
ifstream SA("StudentAnswers.txt");
ifstream CA("CorrectAnswers.txt");char CAArray[20];
char SAArray[20];
// char SA2Array[20];
bool isCorrect;
int correct;
int incorrect;
int counter;
correct = 0;incorrect = 0;
counter = 0;
cout << endl;
if (!SA.fail())
{
cout << "'StudentAnswers.txt' file opened successfully." << endl;
cout << "'CorrectAnswers.txt' file opened successfully." << endl << endl;
int a = 0;
int b = 0;
while (a < 20)
{
CA >> CAArray[a];
a++;
} // while loop to feed char into the array
while (b < 20)
{
SA >> SAArray[b];
b++;
}
} // while loop to feed char into array
CA.close(); // closing "CorrectAnswers.txt"
SA.close(); // closing "StudentAnswers.txt"
GradeReturn(&CAArray[counter], &SAArray[counter], correct, incorrect, counter);
return 0;
}
void GradeReturn(char CAArray[], char SAArray[], int correct, int incorrect, int counter)
{
float percent;
float hundred;
int student;
int catcher[20];
int writeCatcher; int starter;
int catcher_size;
student = 0;
writeCatcher = 0;
catcher_size = ((sizeof catcher) / 4);
while (counter < 20)
{
if ((CAArray[counter]) == (SAArray[counter]))
{
correct++;
cout << "Good job!" << endl;
} // correct handling
else
{
incorrect++;
cout << "You got question " << counter << " wrong." << endl;
counter >> catcher[writeCatcher];
writeCatcher++;
} // incorrect handling
counter++;
} // while loop to determine if a student got a question right or wrong
static_cast <float> (incorrect); // float conversion
cout << endl; // for cleanliness
percent = ((static_cast <float> (correct)) / 20); // percentage
hundred = percent * 100;
PassFail(percent);
if (PassFail(percent) == "pass")
{
student++;
cout << "Report for Student " << student << ":" << endl;
cout << "-----------------------------" << endl;
cout << "Missed " << incorrect << " questions out of 20 for ";
cout << hundred << " % correct." << endl << endl;
starter = 0;
while (starter < (sizeof catcher)
{
if(1=1)
{
catcher_size
}
else
{
cout << "";
starter++;
}
}
}
else if (PassFail(percent) == "fail")
{
student++;
cout << "Missed " << incorrect << " questions out of 20 for ";
cout << hundred << " % correct." << endl << endl;
while (starter < catcher_size)
{
if ((catcher[starter]) == -858993460)
{
starter++;
}
else
{
cout << "";
starter++;
}
}
}
return;
}
string PassFail(float percent)
{
if (percent >= 0.70) // if <pass>
{
return "pass";
}
else // if <fail>
{
return "fail";
}
cout << endl;
}
To get a loop you should keep streams open instead of closing them after reading 20 lines.
As pseudo code that would be:
a = 0;
while(streams_not_empty)
{
CA >> CAArray[a];
SA >> SAArray[a];
++a;
if (a == 20)
{
GradeReturn(&CAArray[counter], &SAArray[counter], correct, incorrect, counter);
a = 0; // Reset a
}
}
CA.close(); // closing "CorrectAnswers.txt"
SA.close(); // closing "StudentAnswers.txt"
You would also need to pass correct, incorrect, counter by reference so that the GradeReturn can change their value and their by do the accumulation.
Like:
void GradeReturn(char CAArray[], char SAArray[], int& correct, int& incorrect, int& counter)
Further you shouldn't rely on being able to read exactly Nx20 lines from the files every time. A file could have, e.g. 108 (5x20 + 8) lines, so you code should be able to handle the with only 8 lines. In other words, don't hard code 20 in your function like while (counter < 20). Instead pass the number of lines to be handled and do while (counter < number_to_handle).
Something like this as pseudo code:
a = 0;
while(streams_not_empty)
{
CA >> CAArray[a];
SA >> SAArray[a];
++a;
if (a == 20)
{
GradeReturn(&CAArray[counter], &SAArray[counter], correct, incorrect, counter, a);
// ^
a = 0; // Reset a
}
}
if (a != 0)
{
// Process the rest
GradeReturn(&CAArray[counter], &SAArray[counter], correct, incorrect, counter, a);
}
CA.close(); // closing "CorrectAnswers.txt"
SA.close(); // closing "StudentAnswers.txt"
One problem you have is you're trying to compare C-style strings with the == operator. This will compare them essentially as if they were pointers to char, i.e. compare whether they point at the same location in memory, not compare the contents of the string. I urge you to look up array-decay and c-string variables to understand more.
Specifically, if (PassFail(percent) == "pass") isn't going to do what you want it to. strcomp doc, strncmp doc using std::string variables instead of c-style strings would all work, but it would be better simply to compare percent to a value, i.e. if(percent >= 0.70 directly instead of calling PassFail and comparing a string.
There are many other issues here also, you at one point call PassFail but do nothing with the return value. The only side affect of PassFail is cout << endl, if that's what you intend, it's a poor decision and hard to read way to put a newline on the console.
Try asking your compiler for more warnings, that's often helpful in finding these types of issues. -Wall -Wextra work for gcc, you may have to read your compiler manual...

C++: When using a value called from a getter method of an object, a random negative int is outputted?

The following is my file TotalTemplate.cpp:
#include <iostream>
#include "conio.h"
using namespace std;
template<class T>
class TotalTemplate{
public:
//protected:
T* items;
int itemsAdded;
int amountOfItems;
//public:
//Exception for trying to add items when its passed its limit
class TooManyItems{ };
//Exception for trying to call total before the total has been reached
class IncompleteTotal{ };
TotalTemplate(int amountOfItems){
TotalTemplate::amountOfItems = amountOfItems;
items = new T[amountOfItems];
itemsAdded = 0;
}
TotalTemplate(int amountOfItems, T firstItem){
TotalTemplate::amountOfItems = amountOfItems;
items[] = new T[amountOfItems];
items[0] = firstItem;
itemsAdded = 1;
}
void addItem(T item){
if (itemsAdded >= amountOfItems)
throw TooManyItems();
else{
items[itemsAdded-1] = item;
itemsAdded++;
}
}
//Returns the amount of items added so far
int getAmountAdded(){
return itemsAdded;
}
T getTotal(){//Here is the method definition that is giving me problems
if (itemsAdded < amountOfItems)
throw IncompleteTotal();
else{
T total=items[0];
for (int i = 1; i < itemsAdded; i++)
total += items[i];
return total;
}
}
};
void main(){
//using int to fill the generic type T
cout << "Enter the amount of items to be totaled: ";
int totalAmountOfItems = getInt();
TotalTemplate<int> *total=new TotalTemplate<int>(totalAmountOfItems);
while (true){
cout << total->getAmountAdded() << " items added so far!\nSelect one of the following actions to take.\n";
cout << "(1) Add an item.\n";
cout << "(2) View total.\n";
cout << "(3) Exit Program.\n";
switch (menuSelect(3)){
case 1://Add an item
try{
cout << "Enter a number to add: ";
int item = getInt();
total->addItem(item);
}
catch (TotalTemplate<int>::TooManyItems){
cout << "\nItems given exceeds expected limit.\n\n";
}
break;
case 2://View Total
try{
int totalInt = total->getTotal(); //Here is my problem
cout << "The total is: " << totalInt << endl<<endl;
}
catch (TotalTemplate<int>::IncompleteTotal){
cout << "\nRunning Total has not yet reached total amount of items yet.\n\n";
}
break;
case 3: //Exit program
return;
}
}
cout << "\n\nExiting program...";
_getch();
}
The problem I'm getting is in the main method, when I call total.getTotal(), instead of return an expected int, being the total of all the items added together, I get a totally random int outputted: -842150451
My guess is that it's outputting something instead of the value returned from getTotal(), but I'm not sure how or why or how to fix it. I come from a Java background so I feel like I'm out of habit doing improper oop C++ practice.
Also, getInt() and menuSelect() are methods I have reused from previous codes multiple times, so I excluded them from the file for simplicity sake.
Does anyone know what I'm doing wrong?
this line in addItem
items[itemsAdded-1] = item;
should be
items[itemsAdded] = item;

Access violation help C++

I am currently getting this error in my code: Unhandled exception at 0x0FF321E8 (msvcp110d.dll) in bankAccountp5.exe: 0xC0000005: Access violation writing location 0xCCCCCCF8. And I'm certain it's to do with an array of objects I have created.
Here is part of my code:
class bankAccount: public bankBranch
{
public:
bankAccount(int, int, int, string, int);
int setAccountNumber ()
{
bankAccountNumber = ++minAccountNumber;
return this->bankAccountNumber;
}
void setAccountBalance ()
{
for(i = 0; i < getNumOfBankAccounts(); i++)
{
cout << "Enter the balance for your bank Account: " << endl;
cin >> accounts[i]->bankAccountBalance;
if (bankAccountBalance > MAX_BALANCE || bankAccountBalance < MIN_BALANCE)
{
cout << "Incorrect bank balance, please try again!" << endl;
--i;
} else {
++i;
}
}
}
void menuSystem(int choice) {
}
void displayBankBranchDetails()
{
cout << "\n";
cout << "DETAILS OF YOUR BANK BRANCH" << endl;
cout << "BSB Number: " << this->getBsbNumber() << endl;
cout << "Address: " << this->getAddress() << endl;
cout << "Postal code: " << this->getPostCode() << endl;
}
void setBankAccountDetails() {
}
int getNumOfBankAccounts() {
return this->numOfBankAccounts;
}
void createBankAccounts()
{
valid = false;
while (valid == false) {
cout << "How many Bank Accounts would you like to create under the Bank Branch BSB: " << getBsbNumber() << endl;
cin >> numOfBankAccounts;
if ( numOfBankAccounts <= MAX_NUMBER_ACCOUNTS)
{
valid = true;
} else {
valid = false;
}
}
}
private:
//bankAccount Data
int bankAccountNumber;
int numOfBankAccounts;
int bankAccountBalance;
int interestRate;
//constants
const static int MAX_NUMBER_ACCOUNTS = 10;
const static int MAX_BALANCE = 100000;
const static int MIN_BALANCE = 0;
//objects
bankBranch homeBranch;
bankAccount* accounts[MAX_NUM_ACCOUNTS];
//misc
int minAccountNumber;
int i;
bool valid;
};
The error occurs when I get to void setAccountBalance(), and I call the array of objects accounts, could anyone help me out with this? Cheers.
You have declared an array of pointers, you have to assign memory to it dynamically, bankAccount* accounts[MAX_NUM_ACCOUNTS];
moreover you don't need to write the else part in setAccountBalance()
This
bankAccount* accounts[MAX_NUM_ACCOUNTS];
creates an array of pointers. You need to take the next step of actually allocating memory for each account. Something like
accounts[some-index] = new bankAccount();
accounts = new bankAccount[MAX_NUM_ACCOUNTS]; needs to be done in one of your functions. You have declared a dynamic array, but still need to initialize it.
EDIT: You should really consider using stl structure vector. This will allow you to just push new accounts into your array and a variety of other things. Using pointers and dynamically allocated arrays need you to manage your memory and other such unnecessary pains.
Check this out: http://www.cplusplus.com/reference/vector/vector/
Add using std::vector after your inclusions. This way you don't need to keep typing std::vector, you can just say vector.
Then, vector<bankAccount> accounts will declare a vector called accounts. When adding new accounts, you can just call accounts.push_back(_______). You can access elements with [] and you also have accounts.size().
As #scottwilson said. Instead use a std::vector<bankAccount> that contain statically allocated bankAccounts.
Else, you might have to allocate memory for each bankAccount pointer, either statically or as you might require, dynamically.
You also need a createBankAccount() function that will allocate this for you whenever you want another bankAccount object:
like so:
public:
void createBankAccount() {
accounts[getNumberOfBankAccounts()] = new bankAccount();
numOfBankAccounts++;
}
Call this function each time you need a new bank account.

Core Segmentation Fault With Vector

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;
}
}
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;