Unexpected heap crash

Hi there!

I have a big problem, this code I wrote works like it should and I'm pretty satisfied with it.
But when I try to exit the console with my menu alternative "exit" I get the following error
HEAP CORRUPTION DETECTED
etc etc
CRT detected that the application wrote to memory after end of heap buffer.


At first I didn't get this error but my read from file function didn't work as it was supposed to, so after a lot of tweaking the read from file function works perfectly but I get this error, and when I exclude the function from the build it works like a charm.

my read from file function
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
void DH::read()
{

	ifstream in("Kostdagböcker.txt");
	string strKcal=" ";
	int kcal=0;
	string strCarb=" ";
	double carb=0.0;
	string strProtein=" ";
	double protein=0.0;
	string strLipid=" ";
	double lipid=0.0;
	string name;
	string usrName;
	string usrName1;
	string usrName2;
	string date;
	string nrs;
	getline(in,nrs);
	this->nrOfDiets=atoi(nrs.c_str());

	if(!in.fail())
	{
		for(int i=0;i<this->nrOfDiets && in.good();i++)
		{
			getline(in,usrName1,' ');
			getline(in,usrName2);
			usrName=usrName1+ " " + usrName2;
			getline(in,date);
			getline(in,name,'\t');
			getline(in,strKcal,'\t');
			getline(in,strCarb,'\t');
			getline(in,strProtein,'\t');
			getline(in,strLipid);
		
			
			kcal=atoi(strKcal.c_str());
			carb=atoi(strCarb.c_str());
			protein=atoi(strProtein.c_str());
			lipid=atoi(strLipid.c_str());

			this->dh[i]=new Diet(name,kcal,protein,carb,lipid,usrName,date);
		}
	}
	else
		cout<<"Gick inte att öppna filen!"<<endl;
	
	in.close();
}


any kind of help is really really appreciated.
What is dh (line 42)?
If it's a vector, consider using push_back instead of operator[] to add elements to it.
Or, since you know the max value of i, you could resize it and then use operator[].
If it's not a vector, consider making it one.
Last edited on
its an array and if I change it into a vector Ill have to change a lot of my other code and on the other hand I'm not that familiar with vectors.
Is it a static array or a dynamic one? In either case, is its size big enough to contain the data you put to it?
its a dynamic array "Test ** test" and no, thats the problem the capacity of the array is set to 3 by my default constructor, I need to make some kind of expansion of the array or somehow send the nrOfDiets value to the capacity.

Seriously, try using vectors instead. You can access a vector the same way you would access an array.
The only changes you'll have to make are on declarations and on argument passing.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <vector>
using namespace std;

template <class T>
ostream & print(const vector<T> & v)
{
    for (int i=0; i<v.size(); i++)
        cout << v[i] << " ";

    return cout;
}

int main()
{
    vector<int> v1;
    vector<double> v2;

    v1.push_back(10);
    v1.push_back(20);
    v1.push_back(30);

    v2.resize(4);

    v2[0]=1.1;
    v2[1]=2.2;
    v2[2]=3.3;
    v2[3]=4.4;

    cout << "int vector:\n";
    print(v1) << endl;

    cout << "double vector:\n";
    print(v2) << endl;

    cout << "\n(hit enter to quit...)";
    cin.get();

    return 0;
}

Useful link -> http://cplusplus.com/reference/stl/vector/

You can, of course, do it without vectors, using pointers and
new[]/delete[] ( http://cplusplus.com/doc/tutorial/dynamic/ ),
but why do it like this when you already have a working solution?
Last edited on
Topic archived. No new replies allowed.