1

I am having an issue with an integer wrapping around to its minimum value unexpectedly.

The value of the integer is 15 before it wraps to -858993460.

Here is the code that is causing this issue:

while(ArrayLocation2 < EmpArray2Size)
{
    GivenEmployees[(*EmployeeSize++)] = curr2;

    prev2 = curr2;
    if(ArrayLocation2 < EmpArray2Size)
    {
        curr1 = EmpArray2[ArrayLocation2];
    }
    ArrayLocation2++;

    if((ArrayLocation2 >= EmpArray2Size) || (prev2.HourlyRate > curr2.HourlyRate))
    {
        subFiles++;
    }
}

If I manually change the values that it needs (16, 17, 18, etc) it works as expected.

Size is declared as int Size = 21; and passed into its current method as &Size if it makes a difference.

Why is this exactly happening?

double-beep
  • 5,031
  • 17
  • 33
  • 41
Matt
  • 619
  • 3
  • 8
  • 23
  • Which integer? There are quite a lot of possibilities in the code. How is each one initialized? Which system are you working on? Does 0xCCCCCCCC have any significance in your program or system? – Jonathan Leffler Mar 14 '11 at 06:27
  • I apologize for the inconsistency in the code - the integer that is wrapping is "EmployeeSize" and is declared as i've described in the OP. Everytime it comes through this method it is reset to 0, and is incremented by 1 each time, when it hits 15, and is incremented one more time it wraps. I am working on a 64 bit Windows 7 system, intel i5. – Matt Mar 14 '11 at 06:27
  • Check you are not passing it by reference anywhere? – deek0146 Mar 14 '11 at 06:31
  • Also check your compiler warnings, and put them on high if they are not already, I have a feeling you might be assigning a pointer to it? – deek0146 Mar 14 '11 at 06:32
  • @Jonathan, @Matt: I think the 0xCCCCCCCC is one of the sentinel values VC++ uses for uninitialized or freed memory. – Jeremiah Willcock Mar 14 '11 at 06:32
  • @Jonathan, @Matt: To follow on, http://stackoverflow.com/questions/370195/when-and-why-will-an-os-initialise-memory-to-0xcd-0xdd-etc-on-malloc-free-new says that 0xCC is uninitialized data. – Jeremiah Willcock Mar 14 '11 at 06:33
  • What's the size of arrays GivenEmployees and EmpArray2? – CygnusX1 Mar 14 '11 at 06:35
  • @Jeremiah: uninitialized data sounds highly plausible for the problem at hand. Thanks for the information. – Jonathan Leffler Mar 14 '11 at 06:36

3 Answers3

3

The problem is that you are incrementing the pointer - and it ends up pointing into random territory.

You probably intended to write:

GivenEmployees[(*EmployeeSize)++] = cur2;

The parentheses are necessary here; they are unnecessary in the original.


From the comments:

The integer that is wrapping is "EmployeeSize" and is declared as I've described in the OP.

(Except that it is called 'Size' in the original OP.) However, it appears to be:

void this_function(..., int *EmployeeSize, ...)
{
    ...code...
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • Thank you very much for pointing that simple error out, I wish i could give you both the answer but it seems Geek you to the draw Jonathan. – Matt Mar 14 '11 at 06:36
  • @Matt: actually, mine is timestamped 06:31:46 and Geekosaur's is timestamped 06:32:02 (so mine is 16 seconds earlier); the order in which answers are presented is random while they have the same vote count. Geekosaur needs the points more than I do - so don't change now. – Jonathan Leffler Mar 14 '11 at 07:07
2

The expression *EmployeeSize++ returns the value pointed to by EmployeeSize and then increments the pointer, not the pointed-to item. Try (*EmployeeSize)++.

geekosaur
  • 59,309
  • 11
  • 123
  • 114
1
GivenEmployees[(*EmployeeSize++)]

Smells like trouble. It is parsed as

GivenEmployees[(*(EmployeeSize++))]

Postfix incrementation has higher precedence than dereferencing. So, you increment a pointer and then dereference it. Is 'EmployeeSize' a pointer to an array?

CygnusX1
  • 20,968
  • 5
  • 65
  • 109