debug assertion failure using pointer

Hi All,

Attempting to read records from a txt file (space delimited) and having problems adding records on the heap. I get an debug assertion failed error (Line 1279) whereas if I just use an array with a static guess of elements it would work just fine (but is not realistic as record entries could be any amount). Code as follows:

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
50
#include <iostream>
#include <fstream>
using namespace std;

struct AccountRecords
{
	int		id;
	char	package;
	float	hrs_used;
	char	billDate[10];
};

void main()
{
//read records from file "CustomerFile.txt" into data structure
	
	ifstream fin;
	fin.open( "CustomerFile.txt" );
	
	if(!fin)	
	{
		cout<<"File open error!"<<endl;
		exit(1);
	}

	int count =0;
        //AccountRecords accounts[50];
	AccountRecords* accounts;
	accounts = new AccountRecords[count];


	for( int i=0; !fin.eof(); i++)
	{
		fin>>accounts[i].id>>accounts[i].package>>accounts[i].hrs_used>>accounts[i].billDate;
		if( !fin.eof() )
			++count;
	}
	
	cout<<"count: "<<count<<endl;

	for( int i =0; i<count; i++){
		cout<< accounts[i].id<<" "
			<< accounts[i].package<<" "
			<< accounts[i].hrs_used<<" "
			<< accounts[i].billDate<<endl;
	}

	fin.close( );
	accounts=0;
}


Here is the data in the text file:
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
101  A  7  07262003
102  a  10  07262003
103  a  11  07262003
104  a  12.5  07262003
105  a  13  07262003
106  a  15  07262003
107  a  18  07262003
108  a  20  07262003
109  a  22  07262003
110  a  25  07262003
111  a  30  07262003
201  B  7  07262003
202  b  10  07262003
203  b  11  07262003
204  b  12.5  07262003
205  b  13  07262003
206  b  15  07262003
207  b  18  07262003
208  b  20  07262003
209  b  22  07262003
210  b  25  07262003
211  b  30  07262003
301  C  7  07262003
302  c  10  07262003
303  c  11  07262003
304  c  12.5  07262003
305  c  13  07262003
306  c  15  07262003
307  c  18  07262003
308  c  20  07262003
309  c  22  07262003
310  c  25  07262003
311  c  30  07262003

The error has something to do with the data structure somehow, but can't determine how to read an unknown amount of records from a text file and not get the error stated.

Thanks in advance!

Geoff-
1
2
3
int count =0;
AccountRecords* accounts;
accounts = new AccountRecords[count];


There's your problem. count = 0, so when the third line is executed, it is allocating sizeof AccountRecord * 0, which = 0. You need to allocate the space for the array after you have counted, such as on line 40. When you've finished with it, because you've allocated the memory yourself, you also need to free it yourself, so when you close the file, do this: delete[] accounts;.
Yes I stated that the record allocation using the pointer seems to make the error at the end of the program (though the records all display as expected). However, what would be your thoughts toward a solution for counting the records prior to adding them to any data store as you mentioned?
Chewbob wrote:
You need to allocate the space for the array after you have counted, such as on line 40.
Topic archived. No new replies allowed.