Re: Strange behavior with pointers

From: Mythran (kip_potter_at_hotmail.comREMOVETRAIL)
Date: 02/16/05


Date: Wed, 16 Feb 2005 08:42:30 -0800


"John" <John@discussions.microsoft.com> wrote in message
news:0B171982-A60E-4666-A7A6-E9E11B940F31@microsoft.com...
>I have created a program that accesses a flat 2 dimensional array and have
> developed a routine to sum the four neighbors of a specific row/column
> position.
>
> Here is an example, this will sum all the values of row 1, column 1 (base
> 0)
> which is the number 5
>
>
> int a[9] = {1,2,3,4,5,6,7,8,9};
> int* pa = &a[3]; // row 1, column 0 (i.e. number 4)
> int n = *(pa++); // n = 4
> n += *(++pa); // n = 10 (4 + 6)
> n += *(--pa - 3); // n = 12 (10 + 2)
> n += *(pa + 3); // n = 20 (12 + 8)
>
> Now here is the strange part if I combine all the code above into one
> statement
>
> n = *(pa++) + *(++pa) + *(--pa - 3) + *(pa + 3);
>
> I don't get the same results, instead of 20 I get 18. Am I missing
> something
> here is this a compiler or programming error?
>
> Any help would be greatly appreciated
>

Something I see right off the bat is...what happens to pa before and after
combining this to the statement? If you follow pa through you get the
following:

int* pa = &a[3]; -- pa == 4
int n = *(pa++); -- pa == 4
n += *(++pa); -- pa == 6
n += *(--pa -3) -- pa == 5
n += *(pa + 3) -- pa == 5

so, putting that together we get: (4 + 6 + 5 + 5)..

Putting it together on one line like you have it means...

n = *(pa++) + *(++pa) + *(--pa - 3) + *(pa + 3) means the following:

n = pa++ (pa holds a value of 4) + (pa holds a value of 6) + (pa holds a
value of 4) - 3 + (pa holds a value of 4) + 3

the first pa++ is evaluated then incremented (use in expression then add 1
to pa)...value used is 4
the ++pa is incremented then evaluated (add 1 to pa then use in
expression)...value used is 6 (previous value used was 4 but previous calc
for pa was pa++ and therefore it was incremented to 5. This ++pa adds 1 to
5 which makes it 6.)
Then, --pa is decremented then evalueated (subtracts 1 from pa [6 - 1] to
make 5) ... value used is 5
adds 3 to 5
Lastly, pa is 5 and it adds 3 to the 5 which makes 8.

so, we get....

(pa++ [pa evaluated as 4]) + ([pa is 5 before this] ++pa [pa is
preincremented and now is + 6]) + ([pa is still 6] --pa [now pa is 5]) - 3 +
([pa is still 5]5) + 3

4 + 6 + 5 - 3 + 5 + 3 = 18

Hope this helps :)

Mythran

ps. haven't done this in years....feels good still being able to do
it...albeit, took me awhile longer than it should have to remember...I'm
getting old!



Relevant Pages

  • Strange behavior with pointers
    ... developed a routine to sum the four neighbors of a specific row/column ...
    (microsoft.public.dotnet.languages.vc)
  • Re: Strange behavior with pointers
    ... >> developed a routine to sum the four neighbors of a specific ... This is one possible way for the compiler to come to this result. ...
    (microsoft.public.dotnet.languages.vc)
  • Re: Strange behavior with pointers
    ... > developed a routine to sum the four neighbors of a specific row/column ... The compiler is allowed to do whatever it likes. ...
    (microsoft.public.dotnet.languages.vc)
  • Re: lsqcurvefit-converge?
    ... > I have a problem considering lsqcurvefit as a routine to fit a number ... Are you truly looking at the sum of the residuals? ... The latter is what lsqcurvefit uses. ...
    (comp.soft-sys.matlab)
  • Creating a UDF from a VBA routine...
    ... How can I convert the below routine to a UDF ... put the target value in cell B1 ... Sum = Sum + Cells ...
    (microsoft.public.excel.programming)