Problem returning an object in an overloaded function

http://codepad.org/WaSOKsu8

There are a few functions with this problem, but a good example of one is at line 248. That function is intended to receive 2 arguments (a playlist object, which includes an array of songs, and a song object, which includes 2 strings) and return 1 variable (a playlist object that equals the inputted song added to the inputted playlist). When I run this function, I get an error saying:
Debug Error!

Invalid allocation size: 4294967295 bytes.


A little tinkering gets me this result:
Windows has trigged a breakpoint in Project 2.exe.

This may be due to a corruption of the heap, which indicates a bug in Project 2.exe or any of the DLLs it has loaded.

This may also be due to the user pressing F12 while Project 2.exe has focus.

The output window may have more diagnostic information.



I've been working on this program for days, and can't figure this out. Any help would be MUCH appreciated.
Looks like you're trying to allocate around 4 Gig of memory
Yes, you're right, but why is that happening? I'm attempting to allocate an array of 3 objects that each have 2 strings. Shouldn't be anywhere close to 4 gigs. Am I not using the destructor correctly? What haven't I destructed that should be?

Edit: The problem seems to be in the assignment overload. ListSize (an int in the PlayList class) returns a ridiculous number in that function...maybe there's some sort of scope problem?
Last edited on
Here's the afflicted code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
PlayList& PlayList::operator=(const PlayList &p) {
//Assignment operator (p1 = p2)
	cout << "*" << p.PlayPtr[0] << endl;
	if (this!=&p) { //if the items aren't the same
		Song *s=new Song[p.ListSize];	//allocate new array of p's size

		this->ListSize=p.ListSize;
		this->index=p.index;
		this->Mode=p.Mode;

		delete [] this->PlayPtr; //deallocate memory in this->PlayPtr
		//copy elements in inputted array into new array
		for (int i=0; i<p.ListSize; i++) s[i]=p.PlayPtr[i];
		this->PlayPtr=s;			//copy new array into old spot
	}
	return *this;
}


and the declaration in the header:
 
	PlayList& operator=(const PlayList &p);		//PlayList assignment operator 
Try checking the value of p.ListSize before line 5. As that's the only part allocating memory.
p.ListSize returns a ridiculous number (-8 million or something). If I could figure out why, I'd finally be finished :(
Try to find where you initialize that variable and you'll have your answer.
The variable initializes in the constructor, along with all the other variables of that class. Furthermore, NONE of the variables inside the class are accessible in the operator.

I can not for the life of me figure out why.

BTW the program takes the result of another function of the class (an overloaded addition operator) and uses it as the argument in the assignment operator.

Here's the newest version of the broken program:
http://codepad.org/OPJdK52w

And, just for reference, the whole project:
http://codepad.org/MEqvdSQg
Last edited on
Have you noticed that while you add five songs in the constructor, you make the list size 3?
Does this copy constructor do anything?
1
2
3
4
5
6
7
8
9
10
11
12
PlayList::PlayList(const PlayList& p) {
//PlayList copy operator (PlayList p1 = p2)
    PlayList t;
    t.index=p.index;
    t.ListSize=p.ListSize;
    t.Mode=p.Mode;

    Song *s=new Song[p.ListSize];
    t.PlayPtr=s;
    for (int i=0; i<p.ListSize; i++) t.PlayPtr[i]=p.PlayPtr[i]; /*copy elements 
                                                        in inputted playlist*/
}
Last edited on
I was about to ask the same thing guestgulkan. You should be editing/constructing the PlayList pointed to by this. You never initialize any of your playlist's fields.
Topic archived. No new replies allowed.