0

I was playing around with pointers and got results I did not expect:

#include <iostream>
#include <vector>

int main() {
    int arr[4] = { 1, 2, 3, 4 };
    int* pArr = arr;
    std::cout << "First number: " << *pArr << " at address: " << pArr;
    pArr++;
    std::cout << "\nSecond number: " << *pArr << " at address: " << pArr;
    pArr++;
    std::cout << "\nThird number: " << *pArr << " at address: " << pArr;
    pArr++;
    std::cout << "\nFourth number: " << *pArr << " at address: " << pArr;
    
    int* pArr2 = arr;
    std::cout << "\n"
        << *pArr2++ << "\n"
        << *pArr2++ << "\n"
        << *pArr2++ << "\n"
        << *pArr2++ << "\n";
    /*
    int* pArr2 = arr;
    std::cout << "\n"
        << ++ * pArr2 << "\n"
        <<  * ++pArr2 << "\n";
    */
}

The two different results:

1 2 3 4 - as expected using the first method

4 3 2 1 - using cout with multiple arguments I do not know the proper name.

So my question is - why does this happen? Using multiple cout statements results in expected for me code, while using just 1 cout results in backwards solution.

Results screencap

As a side note, another confusing thing to me is that pre-increment results in all values being equal. In the commented bit of code, the result is 3 3, no matter the ++ placement with respect to the *.

5
  • Which compiler and version of C++ are you using? Commented Jul 3, 2020 at 16:05
  • 1
    en.cppreference.com/w/cpp/language/eval_order Commented Jul 3, 2020 at 16:05
  • @RobertAndrzejuk I was using Visual Studio, and whatever c++ version was default - switched to newest and started working as commenter below pointed out. Thank you for your link also - got some reading to do :^) Commented Jul 3, 2020 at 16:24
  • By default MSVC is C++14 Commented Jul 3, 2020 at 16:27
  • stackoverflow.com/questions/38501587/… Commented Jul 3, 2020 at 17:28

1 Answer 1

5

This code:

std::cout << "\n" << *pArr2++ << "\n";
std::cout << "\n" << *pArr2++ << "\n";

has a well defined order of modifications to pArr and will print

1
2

But this code:

std::cout << "\n" << *pArr2++ << "\n" << *pArr2++ << "\n";

invokes undefined behavior before c++17, because there are multiple modifications to pArr2 that are unsequenced. The program has UB, so it could print anything.

From c++17, there is a sequence point between the modifications, and the above code is guaranteed to print:

1
2
Sign up to request clarification or add additional context in comments.

1 Comment

cigien, do me a favour and take a look at this one: stackoverflow.com/questions/62666619/… if you have the time. I've been trying to figure out for the past few days if the asker's tripped over a bug in MSVC or if I've missed something.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.