473,465 Members | 1,622 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Strange behavior with pointers

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

Nov 17 '05 #1
5 1048

"John" <Jo**@discussions.microsoft.com> wrote in message
news:0B**********************************@microsof t.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!
Nov 17 '05 #2

"John" <Jo**@discussions.microsoft.com> skrev i meddelandet
news:0B**********************************@microsof t.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?


You cannot modify the same value (pa) more than once in each expression.
The order of evaluation is not defined in C and C++, so you cannot
predict the outcome. The compiler is allowed to do whatever it likes.
Bo Persson
Nov 17 '05 #3

"Mythran" <ki********@hotmail.comREMOVETRAIL> skrev i meddelandet
news:%2****************@TK2MSFTNGP09.phx.gbl...

"John" <Jo**@discussions.microsoft.com> wrote in message
news:0B**********************************@microsof t.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


This is one possible way for the compiler to come to this result.
However, it is not required to chose this particular order. When a
variable is modified more that once in the same expression (formally
"between two sequence points"), any and all results are equally correct
and possible.

For Java, there is probably a defined order of evaluation, but for C and
C++ there is not.
Bo Persson
Nov 17 '05 #4
Woah, disregard my post lol, I have to rethink my thoughts!

Mythran
Nov 17 '05 #5
John wrote:
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?


Programming error. For more, see:

http://www.eskimo.com/~scs/C-faq/q3.2.html
http://www.research.att.com/~bs/bs_f...aluation-order

--
Doug Harrison
Microsoft MVP - Visual C++
Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Bruno van Dooren | last post by:
Hi All, i have some (3) different weird pointer problems that have me stumped. i suspect that the compiler behavior is correct because gcc shows the same results. ...
3
by: Bruno van Dooren | last post by:
Hi All, i have some (3) different weird pointer problems that have me stumped. i suspect that the compiler behavior is correct because gcc shows the same results. ...
11
by: Marlene Stebbins | last post by:
Something very strange is going on here. I don't know if it's a C problem or an implementation problem. The program reads data from a file and loads it into two arrays. When xy, x, y, *xlist and...
5
by: cpptutor2000 | last post by:
I am compiling and running the following code snippet on a Linux box - I am really puzzled by the answers. Could someone please tell me what might be wrong? void test(){ int m = 0; int n = 0;...
2
by: Bruno van Dooren | last post by:
Hi All, i have some (3) different weird pointer problems that have me stumped. i suspect that the compiler behavior is correct because gcc shows the same results. ...
31
by: gamehack | last post by:
Hi all, I've been testing out a small function and surprisingly it does not work okay. Here's the full code listing: #include "stdlib.h" #include "stdio.h" char* escaped_byte_cstr_ref(char...
2
by: Andy Carlson | last post by:
I have a strange problem. I am trying to use a global variable, but I can't get the second routine to recognize changes to the globabl. I finally realize, that I was forking, so the global was...
6
by: zl2k | last post by:
hi, there I have a appendable binary file of complex data structure named data.bin created by myself. It is written in the following format: number of Data, Data array Suppose I have...
160
by: DiAvOl | last post by:
Hello everyone, Please take a look at the following code: #include <stdio.h> typedef struct person { char name; int age; } Person;
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.