Getting an infinite loop when allocating memory

Oct 24, 2012 at 11:47pm
I am creating a program to calculate and display call statistics. When the default constructor is called and it goes to allocate memory for an array for the call stats it falls into an infinite loop. I have identified the problem to be at call_DB = new CALLS[CAPACITY]; but I can't seem to get it fixed. Could someone give me some pointers on what to do?
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
CALLS::CALLS()
{
	this->call_DB = NULL;
	
	cout<<"Default Constructor \n";

	
	ifstream input;
	string filename;

	CAPACITY = 5;

	call_DB = new CALLS[CAPACITY];
	
	count = 0;

	cout<<endl<<"Enter the filename: ";
	cin>>filename;

	input.open(filename.c_str());

	while(!input.eof() && count < CAPACITY)
	{
		
		if (!input.eof())
		{
			input>>call_DB[count].cellPhoneNumber;
			input>>call_DB[count].relays;
			input>>call_DB[count++].minutes;
		}
	}

	input.close();
}
Oct 25, 2012 at 12:45am
new calls constructors on each of the elements of the array, so you fall into infinite recursion.
Basically, don't allocate objects of a given type inside the constructor of that same type.
Oct 25, 2012 at 12:57am
This is a stupid question, but could you give me an example of how to do it?
Oct 25, 2012 at 1:08am
No, because I don't know what you're trying to accomplish.

What you have now (if we pretend it terminates) would create a pental tree structure.
My wild guess is that CALLS should hold an array of some other type, and probably nothing else. This other type is the one that should hold cellPhoneNumber, relays, and minutes.
Last edited on Oct 25, 2012 at 1:09am
Oct 25, 2012 at 1:52am
I figured it out. I created another class in the same header file with records in it and used it in place of CALLS and it compiled fine, but now I am having an issue with my explicit value constructor. It won't let me input items from a file to the an array in it. I am getting this error " error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'call_record' (or there is no acceptable conversion)". It is occurring at "input>>call_DB[count++];".
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
CALLS::CALLS(const string & filename, const int size) 
{
	cout<<"Explicit-value constructor has been called\n";

	ifstream input;

	CAPACITY = size;
	call_DB = new call_record[size];
	count = size;

	input.open(filename.c_str());

	for(int i=0; i<count;i++)
	{
		input>>call_DB[count++];
	}
	input.close();
}
Topic archived. No new replies allowed.