12 Replies - 772 Views - Last Post: 27 September 2018 - 01:13 PM Rate Topic: -----

#1 Afaq Saeed   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 27-September 18

Function gives stack corrupted error when called the third time

Posted 27 September 2018 - 06:14 AM

void PlayPoker :: DealerMove()
{
	
	
	Card besthand[5];
	static int a = 0;
	Card *Possible=NULL;
	int size = (5+a);
	Possible = new Card [size];
	Possible[0] = Hole2[0];
	Possible[1] = Hole2[1];
	cout << "Hole Cards are : \n" << Hole2[0].toString() << "\n" << Hole2[1].toString()<<endl;
	for (int i = 0; i < (3 + a); i++)
		Possible[2 + i] = RefHand[i];
	if (a == 0)
	{
		Copy(besthand, Possible);
		HandWorth a(besthand);
		a.showhand();
		sethand2(besthand);
	}
	Card temp [5];
	if (a == 1)
	{
		for (int one = 0; one < size; one++)
		{
			int t = 0;
			for (int i = 0; i < size; i++)
			{
				if (i != one)
				{
					temp[t] = Possible[i];
					t++;
				}
				if ( (one == 0))
					Copy(besthand, temp);

				else if (Compare(temp, besthand))
					Copy(besthand, temp);
			}
		}
		HandWorth a(besthand);
		a.showhand();
		sethand2(besthand);
	}
	if (a == 2)
	{
		for (int two = 0; two < size;two++)//Premutation loop
			for (int one = 1; one < size; one++)//Second Permutatation loop
			{
			int t = 0;
				for (int i = 0; i < size; i++)//assign values
				{
					if ((i != one)&&(i !=two))
					{
					temp[t] = Possible[i];
					t++;
					}
					
				}
				if ((two == 0) && (one == 1))//Logic For assembling Besthand
					Copy(besthand, temp);

				else if (Compare(temp, besthand))
					Copy(besthand, temp);
			}
		HandWorth ab(besthand);
		cout << "Best ";
		ab.showhand();
		sethand2(besthand);
	}
	delete[] Possible;
	Possible = NULL;
	a++;
}
:code:

Is This A Good Question/Topic? 0
  • +

Replies To: Function gives stack corrupted error when called the third time

#2 Programmer2004   User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 96
  • Joined: 25-October 17

Re: Function gives stack corrupted error when called the third time

Posted 27 September 2018 - 06:28 AM

Does your compiler show at what line does the error occur?
Was This Post Helpful? 0
  • +
  • -

#3 Afaq Saeed   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 27-September 18

Re: Function gives stack corrupted error when called the third time

Posted 27 September 2018 - 06:35 AM

View PostProgrammer2004, on 27 September 2018 - 06:28 AM, said:

Does your compiler show at what line does the error occur?

Yes after a++ line it says stack was corrupted around a
Was This Post Helpful? 0
  • +
  • -

#4 Salem_c   User is online

  • void main'ers are DOOMED
  • member icon

Reputation: 2239
  • View blog
  • Posts: 4,326
  • Joined: 30-May 10

Re: Function gives stack corrupted error when called the third time

Posted 27 September 2018 - 07:35 AM

> static int a = 0;
Why is this static?
Check the value of a when it crashes, it's not going to be what you expect.

> int size = (5+a);
> Possible = new Card [size];
Possible will be right (perhaps)

> Card temp [5];
But temp risks buffer overrun with all those loops limited by 'size'.
Was This Post Helpful? 3
  • +
  • -

#5 Afaq Saeed   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 27-September 18

Re: Function gives stack corrupted error when called the third time

Posted 27 September 2018 - 08:19 AM

What do you mean by buffer overrun?

This post has been edited by Skydiver: 27 September 2018 - 10:17 AM
Reason for edit:: Removed unnecessary quotes. No need to quote the post above yours.

Was This Post Helpful? 0
  • +
  • -

#6 Salem_c   User is online

  • void main'ers are DOOMED
  • member icon

Reputation: 2239
  • View blog
  • Posts: 4,326
  • Joined: 30-May 10

Re: Function gives stack corrupted error when called the third time

Posted 27 September 2018 - 10:16 AM

Get into the habit of pasting terminology you don't understand into a search engine.
https://en.wikipedia...Buffer_overflow
Was This Post Helpful? 3
  • +
  • -

#7 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6502
  • View blog
  • Posts: 22,267
  • Joined: 05-May 12

Re: Function gives stack corrupted error when called the third time

Posted 27 September 2018 - 10:19 AM

A buffer overrun is when you write past the limits of your buffer. For example:
int mynumber[5];
mynumber[5] = 23;    // BUG!!! Buffer overrun here


Was This Post Helpful? 2
  • +
  • -

#8 Afaq Saeed   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 27-September 18

Re: Function gives stack corrupted error when called the third time

Posted 27 September 2018 - 11:44 AM

Okay so after some experimenting the part of the code after if( a==2) ends with an error while the code for a==1 part runs okay thogh its the same code as a==1 with one more for loop is the computer running out of stack memory due so many function calls induced by another for loop?


And by the way thanks everyvone for replying and giving my issue some of your time more help would be appreciated
Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg   User is offline

  • member icon

Reputation: 5594
  • View blog
  • Posts: 17,261
  • Joined: 25-December 09

Re: Function gives stack corrupted error when called the third time

Posted 27 September 2018 - 12:13 PM

Do you realize the the problem really is not the value of 'a' but where that value is used?

And by the way I seriously doubt that your program is running out of stack space. It is probably happening because you are accessing some array out of bounds.


Jim
Was This Post Helpful? 1
  • +
  • -

#10 Afaq Saeed   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 27-September 18

Re: Function gives stack corrupted error when called the third time

Posted 27 September 2018 - 12:44 PM

Thanks guys i just realized what the problem was it was really accessing an array out of bounds
Thank you Everyone 😅😅😅😅😅

Okay now note for future stack error always occur like this if you access array out of bounds
Was This Post Helpful? 0
  • +
  • -

#11 jimblumberg   User is offline

  • member icon

Reputation: 5594
  • View blog
  • Posts: 17,261
  • Joined: 25-December 09

Re: Function gives stack corrupted error when called the third time

Posted 27 September 2018 - 12:53 PM

Quote

Okay now note for future stack error always occur like this if you access array out of bounds


Not always, just sometimes. Isn't Undefined behavior fun?

Jim
Was This Post Helpful? 0
  • +
  • -

#12 Afaq Saeed   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 27-September 18

Re: Function gives stack corrupted error when called the third time

Posted 27 September 2018 - 01:06 PM

Not fun its frustrating for the guy experiecing it However for the person who is solving it. It is indeed fun

This post has been edited by Skydiver: 27 September 2018 - 01:37 PM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

Was This Post Helpful? 0
  • +
  • -

#13 jimblumberg   User is offline

  • member icon

Reputation: 5594
  • View blog
  • Posts: 17,261
  • Joined: 25-December 09

Re: Function gives stack corrupted error when called the third time

Posted 27 September 2018 - 01:13 PM

No, Undefined behavior is not really fun for anyone, useful at times but not really fun. You need to be aware that just because this one little program crashes this will not always be the case. And remember just because a program compiles doesn't mean it is correct.


Jim
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1