# Function gives stack corrupted error when called the third time

Page 1 of 1

## 12 Replies - 793 Views - Last Post: 27 September 2018 - 01:13 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=412883&amp;s=c1a3e601f9c5331b95e36903e6205a91&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Afaq Saeed

Reputation: 0
• 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++;
}
```

Is This A Good Question/Topic? 0

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

### #2 Programmer2004

Reputation: 18
• 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?

### #3 Afaq Saeed

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

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

Posted 27 September 2018 - 06:35 AM

Programmer2004, 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

### #4 Salem_c

• void main'ers are DOOMED

Reputation: 2284
• Posts: 4,376
• 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'.

### #5 Afaq Saeed

Reputation: 0
• 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.

### #6 Salem_c

• void main'ers are DOOMED

Reputation: 2284
• Posts: 4,376
• 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

### #7 Skydiver

• Code herder

Reputation: 6675
• Posts: 22,794
• 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

```

### #8 Afaq Saeed

Reputation: 0
• 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

### #9 jimblumberg

Reputation: 5654
• Posts: 17,356
• 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

### #10 Afaq Saeed

Reputation: 0
• 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

### #11 jimblumberg

Reputation: 5654
• Posts: 17,356
• 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

### #12 Afaq Saeed

Reputation: 0
• 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.

### #13 jimblumberg

Reputation: 5654
• Posts: 17,356
• 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