Reputation: 1
I currently reading a book to refreshing my memory on c++. The chapter I'm on has to do with dynamic memory allocation. I doing a practice problem and I'm having some trouble figuring out what is wrong with my program. The question is
"Write a program that lets users keep track of the last time they talked to each of their friends. Users should be able to add new friends (as many as they want!) and store the number of days ago that they last talked to each friend. Let users update this value (but don't let them put in bogus numbers like negative values). Make it possible to display the list sorted by the names of the friends of by how recently it was since they talked to each friend."
For now I'm just trying to get the program to store the user's input correctly.
It crashes after I enter 5 friends so I'm guessing its writing over the array but the Resize function should take care of that.
He is my code
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
struct Friend
{
string friends;
int days;
};
Friend Resize(Friend* p_array, int* size_of_array);
int main()
{
struct Friend f;
int quit = 1;
int array_size = 5;
int number_of_friends = 0;
Friend *p_array = new Friend [array_size];
while(quit != 0)
{
cout << "Enter a friends name.\n";
cin >> f.friends;
cout << "Enter the number of days sence you last saw them.\n";
cin >> f.days;
cout << "Enter '0' to quit the program.\n";
cin >> quit;
if(array_size == number_of_friends)
{
Resize(p_array, &array_size);
}
p_array[number_of_friends] = f;
number_of_friends++;
}
//print the array
cout << endl;
for(int i = 0; i < sizeof(p_array); i++)
{
cout << p_array[i].friends << " " << p_array[i].days << endl;
}
//delete the array
delete [] p_array;
return 0;
}
Friend Resize(Friend* p_array, int* size_of_array)
{
*size_of_array *= 2;
Friend *p_new_array = new Friend [*size_of_array];
for(int i = 0; i < *size_of_array; i++)
{
p_new_array[i] = p_array[i];
}
delete [] p_array;
p_array = p_new_array;
}
Upvotes: 0
Views: 133
Reputation: 87959
The problem is that although your resize code is kind of OK, it only changes the pointer inside the Resize
function. The pointer in main never gets changed at all. The other error is that you double the size_of_array variable before you copy the array, so you end up copying elements from the old array that don't exist.
Change your function like this
Friend* Resize(Friend* p_array, int* size_of_array)
{
Friend *p_new_array = new Friend [*size_of_array * 2];
for(int i = 0; i < *size_of_array; i++)
{
p_new_array[i] = p_array[i];
}
delete [] p_array;
*size_of_array *= 2;
return p_new_array;
}
And then in main use it like this
if(array_size == number_of_friends)
{
p_array = Resize(p_array, &array_size);
}
This way the Resize function returns the new array, and you assign it to the p_array variable in main.,
Upvotes: 0
Reputation: 183
The crash is due to the resize function. The following line causes the crash:
for(int i = 0; i < *size_of_array; i++)
{
p_new_array[i] = p_array[i];
}
You have doubled the size of size_of_array, but p_array has only 5 elements. So this means that you step out of bounds here.
Upvotes: 0
Reputation: 11482
p_array = p_new_array;
This will assign the local Friend*
parameter to p_new_array
.
Therefor
p_array[number_of_friends] = f;
is an access to an invalid object.
Declare Resize
as
Friend Resize(Friend** p_array, int* size_of_array)
or
Friend Resize(Friend*& p_array, int* size_of_array)
to solve this issue.
Upvotes: 2