Thread: ++*ptr and *ptr++ (Pointer arithmetic)

  1. #1
    Registered User
    Join Date
    Aug 2019
    Location
    inside a singularity
    Posts
    308

    ++*ptr and *ptr++ (Pointer arithmetic)

    Okay, so I've learnt that:

    1) ++*p means incrementing the value of de-referenced p
    2) *p++ means incrementing p to the next address and de-referencing it

    Code:
    #include <iostream>
    
    
    int main (void)
    {
        int My_arr[] = { 1 , 4 };
    
        int* ptr = My_arr;
    
        std::cout << ++*ptr;
        std::cout << " " << *ptr++;
    
        return 0;
    }
    I expected the results to be "2 4" from what I've learnt. But, I'm getting "2 2" instead. I've compiled my code on C::B with latest GCC.

    Am I invoking undefined behaviour by writing the incremental statements with cout?

    [EDIT]
    I feel like an idiot for starting this thread... I figured out what's happening. Doing a "cout << *ptr++" means display "*ptr" first and then increment it. Sorry for wasting the time of anyone who looked through this post...
    [/EDIT]
    Last edited by Zeus_; 09-23-2019 at 02:16 PM.

  2. #2
    Registered User
    Join Date
    Aug 2019
    Location
    inside a singularity
    Posts
    308
    I had this question come in a test a few months back. (Just so I don't seem like a complete idiot, here's the question for beginners in Pointers ) (Don't use a compiler though)

    Code:
    int Ar[] = { 10 , 13 , 1 , 4 , 2 , 6 , 7 } , I;
    
    int *P = Ar;
    
    cout << *P++ << "@";
    
    I = Ar[3] - Ar[2];
    
    cout << ++*P++ << "@\n";
    cout << ++I + *P++ << "@";
    cout << ++*P << "@\n";
    
    for ( ; I >= 0 ; I-=2 )
         cout << Ar[I] << "@";
    I'm guessing this pretty much has different behaviour with different optimisation settings but I don't know. Have fun!

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    I'm guessing this pretty much has different behaviour with different optimisation settings
    Quite likely, since the program invokes undefined behaviour (line 9).

    Also beware of operator precedence with things like ++*P.

  4. #4
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    945
    Quote Originally Posted by jimblumberg View Post
    Quite likely, since the program invokes undefined behaviour (line 9).
    Line 9 actually is not undefined behavior. ++*P++ increments both P and *P once each.

  5. #5
    Registered User
    Join Date
    May 2019
    Posts
    214
    @christop

    What @jimblumberg is saying is correct.

    It is among the "ISO" coding standards to never mix these operations in a single clause that way (Stroustrup/Sutter's site on ISO C++).

  6. #6
    Registered User
    Join Date
    Aug 2019
    Location
    inside a singularity
    Posts
    308
    Very true. I was reading the Stroustrup FAQ and a PDF that I came across on the internet while doing my research on the evolution of C and C++. There's so many ways I've been invoking undefined behaviour in some of my programs that I didn't know about until I read through the PDF which cited many different examples (I would link it here but the PDF exceeds the max file size). There's so much to learn tbh that I have no clue about. Well, its all part of the learning process. I've been so into researching and reading more about programming and game development in the last 2 months that I've started to lack in my core subjects at school (Phy, Chem, Math). It's my last year in school so I need to get back to studying more of my core subjects but programming is so much more interesting.

  7. #7
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    945
    Quote Originally Posted by Niccolo View Post
    @christop

    What @jimblumberg is saying is correct.

    It is among the "ISO" coding standards to never mix these operations in a single clause that way (Stroustrup/Sutter's site on ISO C++).
    But it has defined behavior. There's no ambiguity in ++*P++. It is equivalent to ++*(P++) because of operator precedence. It increments the object pointed to by P and then increments P (it's implementation defined when P is actually incremented, but the result of the expression is defined).

    If the same object were modified more than once between sequence points (with something like P++ - ++P), then it would be undefined, but two different objects are being modified in that statement.

    Can you quote or link to Stroustrup's site where he says that statement is undefined?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Niccolo
    @christop

    What @jimblumberg is saying is correct.
    Actually, jimblumberg said two things, and while the second one is certainly correct advice, christop was talking about the first one, i.e., concerning ++*P++ having undefined behaviour.

    I think christop's analysis is correct: while it is true that the increment of P and the increment of *P are unsequenced relative to each other, the same object is never modified more than once in operations unsequenced relative to each other. Furthermore, the syntax constrains it such that the increment of P must happen as if it happened before the increment of *P, just like how in the expression *p++ the increment of p must happen as if it happened before the dereference of p, even though they are unsequenced relative to each other. Consequently, the behaviour is well defined, although of course the expression remains obfuscated.

    Quote Originally Posted by Niccolo
    It is among the "ISO" coding standards to never mix these operations in a single clause that way (Stroustrup/Sutter's site on ISO C++).
    You're referring to the C++ Core Guidelines, and calling that "ISO" even if in quotes is misleading: it is even less of an ISO standard than an ISO draft standard. (This also means that while any proclamation about undefined behaviour there should be taken very seriously on the strength of Stroustrup et al., it is not necessarily correct, unlike the C++ standard itself.) The guideline about avoiding complex expressions is because of readability and sometimes they get written wrongly such that they result in undefined behaviour, not because such expressions always result in undefined behaviour.
    Last edited by laserlight; 09-28-2019 at 01:45 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer arithmetic
    By GiForce in forum C Programming
    Replies: 5
    Last Post: 10-29-2016, 06:31 AM
  2. pointer arithmetic help
    By ihavesixtoes in forum C Programming
    Replies: 3
    Last Post: 12-20-2010, 08:58 PM
  3. Pointer arithmetic
    By depietro in forum C Programming
    Replies: 13
    Last Post: 03-29-2007, 06:03 AM
  4. Arithmetic on pointer...?
    By Volair in forum C Programming
    Replies: 7
    Last Post: 11-22-2005, 01:58 PM
  5. pointer arithmetic
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-04-2001, 06:45 PM

Tags for this Thread