473,569 Members | 2,735 Online
Bytes | Software Development & Data Engineering Community
+ 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 1054

"John" <Jo**@discussio ns.microsoft.co m> wrote in message
news:0B******** *************** ***********@mic rosoft.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)...v alue 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**@discussio ns.microsoft.co m> skrev i meddelandet
news:0B******** *************** ***********@mic rosoft.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********@hot mail.comREMOVET RAIL> skrev i meddelandet
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..

"John" <Jo**@discussio ns.microsoft.co m> wrote in message
news:0B******** *************** ***********@mic rosoft.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)...v alue 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
1110
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. ---------------------------------------------- //example 1: typedef int t_Array; int _tmain(int argc, _TCHAR* argv)
3
2335
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. ---------------------------------------------- //example 1: typedef int t_Array; int main(int argc, char* argv)
11
1655
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 *ylist are ints or floats there is no apparent problem. If these variables are doubles, the program crashes. Furthermore, the crashes occur only when...
5
1554
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; int i = 0; int j = 0;
2
1528
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. ---------------------------------------------- //example 1: typedef int t_Array; int main(int argc, char* argv)
31
2847
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 byte); int main (int argc, const char * argv)
2
1684
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 probably getting copied. So, I changed it to a pointer, and malloc an int so that it can be changed. But, it still doesn't appear to be working. I...
6
342
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 following data.bin (3 Data appended to 2 Data): 2, data0, data1, 3, data0, data1, data2
160
5776
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;
0
7694
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7609
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7921
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8118
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7666
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6278
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2107
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 we have to send another system
0
936
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.