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.