Reputation: 1
I've have been trying to figure this out for a while now and decided it was time for some outside perspective. I have googled as much as possible and from what I read on similar problems this (I think) has something to do with my allocating less memory than is required. I have gone over my code many times and cannot find where my mistake is.
My professor has wrote a print function and the problem only occurs when that function is called so I have narrowed down my search to this function I wrote.
IntStore& IntStore::operator=(const IntStore& rhs)
{
if(this != &rhs)
{
int* newData = new int[rhs.capacity];
int* newFreq = new int[rhs.capacity];
for(int i=0; i < rhs.used; i++)
{
newData[i] = rhs.data[i];
newFreq[i] = rhs.freq[i];
}
delete [] data;
delete [] freq;
data = newData;
freq = newFreq;
capacity = rhs.capacity;
used = rhs.used;
}
return *this;
}
His Print Function reads:
void print_to_cout(IntStore src)
{
// NOTE:
// - first for-loop below looks silly but is purposely added
// - don't try to remove/disable when doing the assignment
// - Why is it added?
for (int i = 1; i < 2; ++i)
{
src = src;
IntStore copy1;
copy1 = src;
}
int countDist = src.countDistinct();
for (int i = 1; i <= countDist; ++i)
cout << setw(5) << src.valAt(i);
cout << endl;
cout << " (freq)";
for (int i = 1; i <= countDist; ++i)
cout << setw(5) << src.freqAt(i);
cout << endl;
}
When I run the program this is what I get back:
*** glibc detected *** ./a2: free(): invalid next size (fast): 0x0000000005714010 ***
Any help is appreciated and thanks in advance.
I am including the constructors to shows more specific portions of the code to help with my question. As for the print function. I am not sure why it is written the way it is. Everything in the main cpp file was created by the professor and was not to be edited. The only editing we did was to the Instore.cpp file. The header was also pre-made.
#include "IntStore.h"
#include <iostream>
#include <cstdlib>
#include <cassert>
using namespace std;
void IntStore::resize(int new_capacity)
{
if(new_capacity < used)
new_capacity = used;
if(new_capacity < 1)
new_capacity = 1;
capacity = new_capacity;
int* newData = new int[capacity];
int* newFreq = new int[capacity];
for(int i=0; i < used; i++)
{
newData[i] = data[i];
newFreq[i] = freq[i];
}
delete [] data;
delete [] freq;
data = newData;
freq = newFreq;
}
IntStore::IntStore(int init_capacity) : capacity(init_capacity), used(0)
{
if(capacity < 1)
capacity = DEFAULT_CAPACITY;
data = new int(capacity);
freq = new int(capacity);
}
IntStore::IntStore(const IntStore& src) : capacity(src.capacity),
used(src.used)
{
data = new int(capacity);
freq = new int(capacity);
for(int i = 0; i < used; i++)
{
data[i] = src.data[i];
freq[i] = src.freq[i];
}
}
IntStore::~IntStore()
{
delete [] data;
delete [] freq;
}
This is the case for the print function:
case 'p': case 'P':
objectNum = get_object_num();
switch (objectNum)
{
case 1:
if ( is1.empty() )
cout << " is1: (empty)" << endl;
else
{
cout << " is1: (data)";
print_to_cout(is1);
}
break;
case 2:
if ( is2.empty() )
cout << " is2: (empty)" << endl;
else
{
cout << " is2: (data)";
print_to_cout(is2);
}
break;
case 3:
if ( is3.empty() )
cout << " is3: (empty)" << endl;
else
{
cout << " is3: (data)";
print_to_cout(is3);
}
}
Upvotes: 0
Views: 121
Reputation: 18972
In your copy constructor you have new int(capacity)
where you should have new int[capacity]
. These are not the same thing at all.
You might consider using the copy and swap idiom for your assignment operator - what you have is mostly correct but rather horrible, and could be a lot shorter and simpler.
Upvotes: 1