Advertisement

Debug Error! When using delete[] ?

Started by August 07, 2002 01:38 AM
9 comments, last by DrunkenBastard 22 years, 4 months ago
Hello After a while of lurking I finally registered so this is my first post. I'm making a Black Jack game and am having a problem with delete[]. Anyway I'll show you my code first: The Black Jack Game: (Ive left out everything I felt was unnessicerry)
      

int BlackJack(int money){
	int *deck;
	
	deck = Shuffle();
	
	delete[] deck;

	cout << "Thats All right now Folks" << endl;

	return money;

}



int* Shuffle(void){
	

	int i; //Loop counter


	int *deck = new int[52];

	for(i = 0; i < 52; i++)
	{
		deck[i] = i;
		//cout << deck &lt;&lt; endl;
</font>
	}
	<font color="gray">//theres actually 52 cards in a deck so 0-51 == 52 ints
</font>
	<font color="gray">//Later I will / the card num by 13 to find out suit
</font>

<font color="gray">//////////////////////   SHUFFLE DECK ////////////////////////////////////////////
</font>
	<font color="gray">//I want to get 2 rand nums between 0-51
</font>
	<font color="gray">//Then flip those 2 nums in the deck
</font>
	<font color="blue">int</font> num1;
	<font color="blue">int</font> num2;
	<font color="blue">int</font> Old1;
	<font color="blue">int</font> Old2;
	<font color="blue">for</font>(i = 0; i &lt; 52; i++)
	{

		num1 = rand()%52;
		num2 = rand()%52;

		<font color="blue">if</font>(num1 == num2)
		{
			–i;
		}
		<font color="blue">else</font>
		{
		<font color="gray">//cout &lt;&lt; num1 &lt;&lt; " " &lt;&lt; num2 &lt;&lt; endl;
</font>
			Old1 = deck[<font color="purple">num1</font>];
			Old2 = deck[<font color="purple">num2</font>];

			deck[<font color="purple">num1</font>] = Old2;
			deck[<font color="purple">num2</font>] = Old1;
		}
	}

	<font color="gray">//Check the shuffling
</font>

	<font color="gray">//for(i = 0; i &lt; 52; i++)
</font>
	<font color="gray">//{
</font>
	<font color="gray">//	cout &lt;&lt; i &lt;&lt; ": " &lt;&lt; deck &lt;&lt; endl;
</font>
	<font color="gray">//}
</font>
	
        deck[<font color="purple">52</font>] = 0;
	<font color="blue">return</font> deck;

}

    

When I step through the code I get <font color="blue">this</font> error on the <font color="darkred">"delete[] deck;"</font> line.
error:
      
Debug Error!

Program: (program name)

DAMAGE: after Normal block(#73) at 0x003016F0

(Press Re<font color="blue">try</font> to debug the application)
       </pre></DIV><!–ENDSCRIPT–> 

The funny thing is is if I take out the delete[] line it runs fine, but I don't feel right leaving delete[] out
I've searched around here and on msdn and got nothing so if any of you can help I would be verry thankful. <img src="wink.gif" width=15 height=15 align=middle>


NOTE: I fixed it by changing int *deck = new int[51] to int *deck = new int[52] in Shuffle(), but why did that work?!?!?

p.s. !@#@!#$$!!! Now it's not working again. Same error. I'm really confused now. <img src="sad.gif" width=15 height=15 align=middle> <img src="sad.gif" width=15 height=15 align=middle> <img src="sad.gif" width=15 height=15 align=middle>

<SPAN CLASS=editedby>[edited by - DrunkenBastard on August 7, 2002 2:40:44 AM]</SPAN>

<SPAN CLASS=editedby>[edited by - DrunkenBastard on August 7, 2002 2:41:22 AM]</SPAN>

<SPAN CLASS=editedby>[edited by - DrunkenBastard on August 7, 2002 3:13:12 AM]</SPAN>    
whell ima the only up people up at 2 am.
I aint got no answer.
Advertisement


[edited by - SysOp_1101 on August 7, 2002 5:42:25 AM]
SysOp_1101
quote: Original post by DrunkenBastard
I''m making a Black Jack game and am having a problem with delete[].

I didn''t check your code very thoroughly but this jumped out...
        deck[52] = 0; 

There is no deck[52] - you allocated 52 elements, so the last one will be deck[51].

quote:
DAMAGE: after Normal block(#73) at 0x003016F0

This almost always means you have clobbered the heap by over-writing an array bounds, which is what deck[52]=0 is guilty of.
On another note - DrunkenBastard, do yourself a favour and learn how to use std::vector in preference to arrays.

[edited by - SabreMan on August 7, 2002 5:48:37 AM]
Hehe, this is why I don't often make replies at 5:00am. Disregard my previous statements as an example of sleep deprivation. Sorry bout the confusion. The duplicated 'deck' variable did it. :D

[edited by - SysOp_1101 on August 7, 2002 5:46:52 AM]
SysOp_1101
Advertisement
quote: Original post by SysOp_1101
Disregard my previous statements as an example of sleep deprivation.

Disregarded! Don''t worry, we all do it at times!
slaps self repediatly then drinks beer.

doh!!!

Thank you SysOp_1101 for replying. That was deffinantly the problem.

And Saberman I will look into std::vector. Thank you all for the comments.

[edited by - DrunkenBastard on August 7, 2002 6:22:07 AM]
Woo Hoo.

I am got done wit teh Black Jack mi first game. and now I am gona maek teh Quake gamez wit mi 31337 skillz. anyone want to help me. all I am needing iz all the, whats them thing name, og ya algorithmz. Then ILL 0wnz j00 all.
fantasies,fantasies,fantasies... wished it was that easy.

.lick

This topic is closed to new replies.

Advertisement