stack around the variable...error

Pages: 12
Hi all,

I'm having an issue with a program I'm writing.. I keep getting a debug error:
Run-Time Check Failure #2 - Stack around the variable 'numTimes' was corrupted.

I have tried a bunch of different ways of using that variable and it either results in the above error or an app hang.

the variable is declared as

int numTimes[23];

the variable is then passed into a function to have its members filled
with an int passed to each member in a loop by another function.

I have stepped through the program several times with the IDE and each time numTimes[i] is holding an int value that would be expected to be returned by the function. a zero or higher.. int value.. and the loop is cut off once i reaches 22.

Last edited on
So far it seems to me like its just getting irritated from changing the value in numTimes
so often... I moved it to section of the loops that only gets run if matches are found so the numTimes array is only filled at that point now.. which reduced the amount of times the value was altered. But now its seems that my application runs peferectly until about 3 or 4 consecutive runs. Then it gives the same error... Any1 have any suggestions?
We should probably see some code, very difficult to say without it. How are you passing the array to the function that assigns the values to it?
You're stepping outside of the array bounds somewhere.

Either that or your writing to a bad pointer.

what makes something a bad pointer?

basic setup of how this is working:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int numTimes[23];
int sortedLength;
set newSet;

newSet = SortSet(dirtySet, numTimes, sortedLength);


set SortSet(set dirtySet, int Times[], int &sortedLength)
{
      tempTimes = inSet(tempSet, dirtySet);//returns int 0-23
      
      //when something is found
      Times[i] = tempTimes;

      //Once were done we also update sortedLength of new array
      sortedLength = i;

     return newSet;
}
Last edited on
what makes something a bad pointer?


A pointer is bad if it points to memory/an object that isn't valid.

basic setup of how this is working:


Abridged code doesn't really help. We already know what the problem is (see my previous post), we would just need to see the code to be able to spot exactly where it is. But if you give a summary, that doesn't really do anything for us.
sigh ok, you asked for it


1
2
[REMOVED]
thanks for help all!
Last edited on
i feel naked
I ran this code without any problems. Where are you getting an error message?
Last edited on
run it like 10 times the same option
which option? :p I tried everyone except 2, which you havent implemented yet

EDIT:

Oh wait, I see. I'll take a closer look:)
Last edited on
3... I believe the problem lies somewhere in the Sorting function: SortBdaySet

Last edited on
I thought so too. Sorry, if I were on my linux-machine right now I'd put it through valgrind and it would probably be a lot easier:p
EDIT:

Know what, give me 10 minutes and I'll be able to debug this a lot easier. Just need a download to finish, then I can reboot into linux, I s**k at doing this stuff on windows:p
Last edited on
i've got no clue i've been struggling with this bit for awhile now.
Ok, so this is not a crash, it's an infinite loop. Somehow, for some bdaysets, the variable i isnt being incemented in the sort-function, so that the loop at while(i < 23) never ends. Now, I havent yet figured out why:/

Well, this is the only error I'm getting, an infinite loop. I'm not getting the error-message you get.
Last edited on
ok so maybe the problem is with the logic for the section that checks for when the cleanset is full or when the sorter has run out of needles.

EDIT: i haven't been getting the stack error since I rearraged the times variables a bit in the loop. But i must have been having both issues before and didn't notice they were different.
Last edited on
Yup, that makes sense. I'm not quite clear on when i is being incremented?
Last edited on
If it helps, it always gets stuck at i=21 and startD is incremented infinitely, far past any value that makes sense (and therefore the loop below that could increment i is never entered).

When there are too many steps/iterations to step through a program line-by-line, you should simply print various values at certain points of the program to get an idea of what's happening (or simply an indicator that a certain code point has been reached).
Last edited on
I wasn't able to reproduce the "stack around variable X corrupted" error, but the program was deadlocking.

After a bit of debugging I found one problem in SortBdaySet. The problem comes from when the same birthday appears multiple times in the set.

Take a look at this:

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
				//Count how many times the birthday appears
				//in the set
				tempTimes = InBdaySet(tempBday, dirtySet);

				if(tempTimes > 0)
				{
					//Make sure the set has not already been added
					int inSet = InBdaySet(tempBday, cleanSet);
					if(inSet == 0)
					{
						//found (next)earliest birthday
						//plug into cleanSet
						cleanSet.birthday[i].m = m;
						cleanSet.birthday[i].d = d;

						//Set the number of times it was found
						Times[i] = tempTimes;

						//Set Last bDay found
						lastBday.d = d;
						lastBday.m = m;

						//Increment Array Element counter
						sortedLength++;

						//Increment the loop
						i++;
					}
				}


Look at what you're doing here. You count how many times the bday is in the dirty set. You then add it to the clean set and increment i only if it isn't already in the clean set.

But what if it is in the clean set already? In that case, i doesn't get incremented and your sort loop effectively becomes "stuck". It can't move on to the next birthday.

After changing the i++ to i += tempTimes; I got around the deadlock, but then the output of the program was a little screwy.



Also the way you're sorting is a little nuts. You're stepping through each day in the year (all 365 of them) to see if they match one of the given dates. It would be easier to just find the lowest birthday in the set, remove it from the dirty set and add it to the clean set.

EDIT:

Also a tip to help reproduce the bug more reliably. When debugging, don't seed with the time. Instead seed with a fixed constant. That way you get the same numbers every time the program is run. Then you can know exactly when the bug will happen.

For example, I seeded with '15' and the bug occured the 3rd time I selected the sort option. This made it easier because I could select the option twice, then set breakpoints, then when I ran it the third time I could step through it knowing it was going to screw up somewhere.
Last edited on
Thanks for the tip, Disch! Never thought of that:)
Pages: 12