Is ++*ptr++ undefined behaviour in c++?












48














I have been asked the following question in a test (I didn't want to write it myself. The test asked it. I know its bad code still) about evaluating ++*ptr++



int Ar[ ] = { 6 , 3 , 8 , 10 , 4 , 6 , 7} ;
int *Ptr = Ar ;
cout<<++*Ptr++ ;


However I suspect this is undefined behaviour since it can be both (++*ptr)++ or ++(*ptr++).
Is it? I am not too well acquainted with documentations so I couldn't find anything.










share|improve this question




















  • 69




    What did C++ do to you that you feel compelled to do this?
    – tadman
    Dec 20 at 18:40






  • 3




    When asking about "undefined behaviour" the best approach is to look in the documentation first.
    – tadman
    Dec 20 at 18:41






  • 4




    @soham: Generally speaking, asking about complex interactions of multiple ++ instances and such, which represents code that should never be written, will attract downvotes. Or to put it another way, if you have to ask if it's well-defined, don't write it that way.
    – Nicol Bolas
    Dec 20 at 18:41








  • 7




    @soham Why do you think it's undefined? I think you'd get less downvotes if you explained that in the question.
    – HolyBlackCat
    Dec 20 at 18:42








  • 4




    @MatteoItalia This was asked in a test, sir ... I didn't want to write it myself
    – SHM
    Dec 20 at 18:43
















48














I have been asked the following question in a test (I didn't want to write it myself. The test asked it. I know its bad code still) about evaluating ++*ptr++



int Ar[ ] = { 6 , 3 , 8 , 10 , 4 , 6 , 7} ;
int *Ptr = Ar ;
cout<<++*Ptr++ ;


However I suspect this is undefined behaviour since it can be both (++*ptr)++ or ++(*ptr++).
Is it? I am not too well acquainted with documentations so I couldn't find anything.










share|improve this question




















  • 69




    What did C++ do to you that you feel compelled to do this?
    – tadman
    Dec 20 at 18:40






  • 3




    When asking about "undefined behaviour" the best approach is to look in the documentation first.
    – tadman
    Dec 20 at 18:41






  • 4




    @soham: Generally speaking, asking about complex interactions of multiple ++ instances and such, which represents code that should never be written, will attract downvotes. Or to put it another way, if you have to ask if it's well-defined, don't write it that way.
    – Nicol Bolas
    Dec 20 at 18:41








  • 7




    @soham Why do you think it's undefined? I think you'd get less downvotes if you explained that in the question.
    – HolyBlackCat
    Dec 20 at 18:42








  • 4




    @MatteoItalia This was asked in a test, sir ... I didn't want to write it myself
    – SHM
    Dec 20 at 18:43














48












48








48


6





I have been asked the following question in a test (I didn't want to write it myself. The test asked it. I know its bad code still) about evaluating ++*ptr++



int Ar[ ] = { 6 , 3 , 8 , 10 , 4 , 6 , 7} ;
int *Ptr = Ar ;
cout<<++*Ptr++ ;


However I suspect this is undefined behaviour since it can be both (++*ptr)++ or ++(*ptr++).
Is it? I am not too well acquainted with documentations so I couldn't find anything.










share|improve this question















I have been asked the following question in a test (I didn't want to write it myself. The test asked it. I know its bad code still) about evaluating ++*ptr++



int Ar[ ] = { 6 , 3 , 8 , 10 , 4 , 6 , 7} ;
int *Ptr = Ar ;
cout<<++*Ptr++ ;


However I suspect this is undefined behaviour since it can be both (++*ptr)++ or ++(*ptr++).
Is it? I am not too well acquainted with documentations so I couldn't find anything.







c++ language-lawyer






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 20 at 18:51

























asked Dec 20 at 18:39









SHM

387310




387310








  • 69




    What did C++ do to you that you feel compelled to do this?
    – tadman
    Dec 20 at 18:40






  • 3




    When asking about "undefined behaviour" the best approach is to look in the documentation first.
    – tadman
    Dec 20 at 18:41






  • 4




    @soham: Generally speaking, asking about complex interactions of multiple ++ instances and such, which represents code that should never be written, will attract downvotes. Or to put it another way, if you have to ask if it's well-defined, don't write it that way.
    – Nicol Bolas
    Dec 20 at 18:41








  • 7




    @soham Why do you think it's undefined? I think you'd get less downvotes if you explained that in the question.
    – HolyBlackCat
    Dec 20 at 18:42








  • 4




    @MatteoItalia This was asked in a test, sir ... I didn't want to write it myself
    – SHM
    Dec 20 at 18:43














  • 69




    What did C++ do to you that you feel compelled to do this?
    – tadman
    Dec 20 at 18:40






  • 3




    When asking about "undefined behaviour" the best approach is to look in the documentation first.
    – tadman
    Dec 20 at 18:41






  • 4




    @soham: Generally speaking, asking about complex interactions of multiple ++ instances and such, which represents code that should never be written, will attract downvotes. Or to put it another way, if you have to ask if it's well-defined, don't write it that way.
    – Nicol Bolas
    Dec 20 at 18:41








  • 7




    @soham Why do you think it's undefined? I think you'd get less downvotes if you explained that in the question.
    – HolyBlackCat
    Dec 20 at 18:42








  • 4




    @MatteoItalia This was asked in a test, sir ... I didn't want to write it myself
    – SHM
    Dec 20 at 18:43








69




69




What did C++ do to you that you feel compelled to do this?
– tadman
Dec 20 at 18:40




What did C++ do to you that you feel compelled to do this?
– tadman
Dec 20 at 18:40




3




3




When asking about "undefined behaviour" the best approach is to look in the documentation first.
– tadman
Dec 20 at 18:41




When asking about "undefined behaviour" the best approach is to look in the documentation first.
– tadman
Dec 20 at 18:41




4




4




@soham: Generally speaking, asking about complex interactions of multiple ++ instances and such, which represents code that should never be written, will attract downvotes. Or to put it another way, if you have to ask if it's well-defined, don't write it that way.
– Nicol Bolas
Dec 20 at 18:41






@soham: Generally speaking, asking about complex interactions of multiple ++ instances and such, which represents code that should never be written, will attract downvotes. Or to put it another way, if you have to ask if it's well-defined, don't write it that way.
– Nicol Bolas
Dec 20 at 18:41






7




7




@soham Why do you think it's undefined? I think you'd get less downvotes if you explained that in the question.
– HolyBlackCat
Dec 20 at 18:42






@soham Why do you think it's undefined? I think you'd get less downvotes if you explained that in the question.
– HolyBlackCat
Dec 20 at 18:42






4




4




@MatteoItalia This was asked in a test, sir ... I didn't want to write it myself
– SHM
Dec 20 at 18:43




@MatteoItalia This was asked in a test, sir ... I didn't want to write it myself
– SHM
Dec 20 at 18:43












2 Answers
2






active

oldest

votes


















67















However I suspect this is undefined behaviour since it can be both (++*ptr)++ or ++(*ptr++). Is it?




Not really, unlike the runtime behavior, which gives ample leeway to implementors, in C++ parsing itself follows quite strict and well-defined rules1. Indeed, looking at the precedence rules, ++*Ptr++ is actually parsed as ++(*(Ptr++)).



This trick question instead is probably alluding to the undefined behavior of expressions such as i = ++i + ++i, where you have a value that appears multiple times in an expression, and is subjected to a modification by a side-effect of the expression itself. Such expressions are illegal, as, unless there's some operator that sequences the side effects2, the exact moment in which they are applied is not defined, so it's undefined exactly what values i would assume in the various points of the expression.



Still, there's no undefined behavior here, as all side effects in the expression operate on different values, which appear only once in the expression: the "inner" ++ affects Ptr, while the outer one affects the value pointed originally by Ptr, i.e. Ar[0].



++(*(Ptr++))
^^^^^____increments Ptr, returning its original value
^^^^^^^^______dereferences the original Ptr, AKA &Ar[0]
^^^^^^^^^^^^_______ increments Ar[0]


That being said, if I ever saw such an expression in a code base of ours I'd go to great lengths to find the author and make sure that this wouldn't happen again.






  1. If sometimes very bizarre and absurdly costly to implement. Still, there are instances of undefined behavior in the standard describing some corner cases of the parsing, but it's orders of magnitude less pervasive than "runtime" undefined behavior.

  2. A handy summary of those rules can be found here; interestingly, some extra guarantees have been added in C++17.






share|improve this answer























  • @val what made it legal?
    – opa
    Dec 20 at 20:28






  • 1




    The behavior of i = ++i + ++i is undefined in C++17
    – Jans
    Dec 20 at 20:32








  • 3




    @val: just checked again: it's not legal. C++17 just added a sequence point for assignment and compound assignment operators. stackoverflow.com/a/46171943/214671 ++i + ++i remains illegal even just by itself (without assignment on the left).
    – Matteo Italia
    Dec 20 at 20:33








  • 2




    @ruakh It's undefined because it cannot be defined. It carries too much ambiguity. The question "Why is it undefined" is because you can't define it.
    – Nelson
    Dec 21 at 5:46






  • 3




    @ruakh: ultimately, it's undefined because the standard says it's undefined; a good rationalization is that the standard left so much leeway for implementors to apply side-effects whenever and however they feel best that they went all the way and left it completely undefined to allow for any possible optimization (say, store half a value at some moment and the other half later, generating a trap representation in the meantime).
    – Matteo Italia
    Dec 21 at 6:59



















20














This



++*Ptr++;


doesn't cause U.B and its evaluated as ++(*(Ptr++))





  • ptr++; /* address post incremented i.e doesn't change here itself */


  • *ptr; /* dereference same address i.e value at location where ptr earlier points i.e 6 */


  • ++*ptr; /* value changed where ptr points i.e Ar[0] becomes 7 */


Note that post increments Ptr++ evaluated as





  • Ptr; /* Ptr doesn't change here itself in same expression */


  • Ptr = Ptr + 1; /* in next expression, Ptr considers the incremented one */






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53874307%2fis-ptr-undefined-behaviour-in-c%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    67















    However I suspect this is undefined behaviour since it can be both (++*ptr)++ or ++(*ptr++). Is it?




    Not really, unlike the runtime behavior, which gives ample leeway to implementors, in C++ parsing itself follows quite strict and well-defined rules1. Indeed, looking at the precedence rules, ++*Ptr++ is actually parsed as ++(*(Ptr++)).



    This trick question instead is probably alluding to the undefined behavior of expressions such as i = ++i + ++i, where you have a value that appears multiple times in an expression, and is subjected to a modification by a side-effect of the expression itself. Such expressions are illegal, as, unless there's some operator that sequences the side effects2, the exact moment in which they are applied is not defined, so it's undefined exactly what values i would assume in the various points of the expression.



    Still, there's no undefined behavior here, as all side effects in the expression operate on different values, which appear only once in the expression: the "inner" ++ affects Ptr, while the outer one affects the value pointed originally by Ptr, i.e. Ar[0].



    ++(*(Ptr++))
    ^^^^^____increments Ptr, returning its original value
    ^^^^^^^^______dereferences the original Ptr, AKA &Ar[0]
    ^^^^^^^^^^^^_______ increments Ar[0]


    That being said, if I ever saw such an expression in a code base of ours I'd go to great lengths to find the author and make sure that this wouldn't happen again.






    1. If sometimes very bizarre and absurdly costly to implement. Still, there are instances of undefined behavior in the standard describing some corner cases of the parsing, but it's orders of magnitude less pervasive than "runtime" undefined behavior.

    2. A handy summary of those rules can be found here; interestingly, some extra guarantees have been added in C++17.






    share|improve this answer























    • @val what made it legal?
      – opa
      Dec 20 at 20:28






    • 1




      The behavior of i = ++i + ++i is undefined in C++17
      – Jans
      Dec 20 at 20:32








    • 3




      @val: just checked again: it's not legal. C++17 just added a sequence point for assignment and compound assignment operators. stackoverflow.com/a/46171943/214671 ++i + ++i remains illegal even just by itself (without assignment on the left).
      – Matteo Italia
      Dec 20 at 20:33








    • 2




      @ruakh It's undefined because it cannot be defined. It carries too much ambiguity. The question "Why is it undefined" is because you can't define it.
      – Nelson
      Dec 21 at 5:46






    • 3




      @ruakh: ultimately, it's undefined because the standard says it's undefined; a good rationalization is that the standard left so much leeway for implementors to apply side-effects whenever and however they feel best that they went all the way and left it completely undefined to allow for any possible optimization (say, store half a value at some moment and the other half later, generating a trap representation in the meantime).
      – Matteo Italia
      Dec 21 at 6:59
















    67















    However I suspect this is undefined behaviour since it can be both (++*ptr)++ or ++(*ptr++). Is it?




    Not really, unlike the runtime behavior, which gives ample leeway to implementors, in C++ parsing itself follows quite strict and well-defined rules1. Indeed, looking at the precedence rules, ++*Ptr++ is actually parsed as ++(*(Ptr++)).



    This trick question instead is probably alluding to the undefined behavior of expressions such as i = ++i + ++i, where you have a value that appears multiple times in an expression, and is subjected to a modification by a side-effect of the expression itself. Such expressions are illegal, as, unless there's some operator that sequences the side effects2, the exact moment in which they are applied is not defined, so it's undefined exactly what values i would assume in the various points of the expression.



    Still, there's no undefined behavior here, as all side effects in the expression operate on different values, which appear only once in the expression: the "inner" ++ affects Ptr, while the outer one affects the value pointed originally by Ptr, i.e. Ar[0].



    ++(*(Ptr++))
    ^^^^^____increments Ptr, returning its original value
    ^^^^^^^^______dereferences the original Ptr, AKA &Ar[0]
    ^^^^^^^^^^^^_______ increments Ar[0]


    That being said, if I ever saw such an expression in a code base of ours I'd go to great lengths to find the author and make sure that this wouldn't happen again.






    1. If sometimes very bizarre and absurdly costly to implement. Still, there are instances of undefined behavior in the standard describing some corner cases of the parsing, but it's orders of magnitude less pervasive than "runtime" undefined behavior.

    2. A handy summary of those rules can be found here; interestingly, some extra guarantees have been added in C++17.






    share|improve this answer























    • @val what made it legal?
      – opa
      Dec 20 at 20:28






    • 1




      The behavior of i = ++i + ++i is undefined in C++17
      – Jans
      Dec 20 at 20:32








    • 3




      @val: just checked again: it's not legal. C++17 just added a sequence point for assignment and compound assignment operators. stackoverflow.com/a/46171943/214671 ++i + ++i remains illegal even just by itself (without assignment on the left).
      – Matteo Italia
      Dec 20 at 20:33








    • 2




      @ruakh It's undefined because it cannot be defined. It carries too much ambiguity. The question "Why is it undefined" is because you can't define it.
      – Nelson
      Dec 21 at 5:46






    • 3




      @ruakh: ultimately, it's undefined because the standard says it's undefined; a good rationalization is that the standard left so much leeway for implementors to apply side-effects whenever and however they feel best that they went all the way and left it completely undefined to allow for any possible optimization (say, store half a value at some moment and the other half later, generating a trap representation in the meantime).
      – Matteo Italia
      Dec 21 at 6:59














    67












    67








    67







    However I suspect this is undefined behaviour since it can be both (++*ptr)++ or ++(*ptr++). Is it?




    Not really, unlike the runtime behavior, which gives ample leeway to implementors, in C++ parsing itself follows quite strict and well-defined rules1. Indeed, looking at the precedence rules, ++*Ptr++ is actually parsed as ++(*(Ptr++)).



    This trick question instead is probably alluding to the undefined behavior of expressions such as i = ++i + ++i, where you have a value that appears multiple times in an expression, and is subjected to a modification by a side-effect of the expression itself. Such expressions are illegal, as, unless there's some operator that sequences the side effects2, the exact moment in which they are applied is not defined, so it's undefined exactly what values i would assume in the various points of the expression.



    Still, there's no undefined behavior here, as all side effects in the expression operate on different values, which appear only once in the expression: the "inner" ++ affects Ptr, while the outer one affects the value pointed originally by Ptr, i.e. Ar[0].



    ++(*(Ptr++))
    ^^^^^____increments Ptr, returning its original value
    ^^^^^^^^______dereferences the original Ptr, AKA &Ar[0]
    ^^^^^^^^^^^^_______ increments Ar[0]


    That being said, if I ever saw such an expression in a code base of ours I'd go to great lengths to find the author and make sure that this wouldn't happen again.






    1. If sometimes very bizarre and absurdly costly to implement. Still, there are instances of undefined behavior in the standard describing some corner cases of the parsing, but it's orders of magnitude less pervasive than "runtime" undefined behavior.

    2. A handy summary of those rules can be found here; interestingly, some extra guarantees have been added in C++17.






    share|improve this answer















    However I suspect this is undefined behaviour since it can be both (++*ptr)++ or ++(*ptr++). Is it?




    Not really, unlike the runtime behavior, which gives ample leeway to implementors, in C++ parsing itself follows quite strict and well-defined rules1. Indeed, looking at the precedence rules, ++*Ptr++ is actually parsed as ++(*(Ptr++)).



    This trick question instead is probably alluding to the undefined behavior of expressions such as i = ++i + ++i, where you have a value that appears multiple times in an expression, and is subjected to a modification by a side-effect of the expression itself. Such expressions are illegal, as, unless there's some operator that sequences the side effects2, the exact moment in which they are applied is not defined, so it's undefined exactly what values i would assume in the various points of the expression.



    Still, there's no undefined behavior here, as all side effects in the expression operate on different values, which appear only once in the expression: the "inner" ++ affects Ptr, while the outer one affects the value pointed originally by Ptr, i.e. Ar[0].



    ++(*(Ptr++))
    ^^^^^____increments Ptr, returning its original value
    ^^^^^^^^______dereferences the original Ptr, AKA &Ar[0]
    ^^^^^^^^^^^^_______ increments Ar[0]


    That being said, if I ever saw such an expression in a code base of ours I'd go to great lengths to find the author and make sure that this wouldn't happen again.






    1. If sometimes very bizarre and absurdly costly to implement. Still, there are instances of undefined behavior in the standard describing some corner cases of the parsing, but it's orders of magnitude less pervasive than "runtime" undefined behavior.

    2. A handy summary of those rules can be found here; interestingly, some extra guarantees have been added in C++17.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Dec 21 at 7:08

























    answered Dec 20 at 18:43









    Matteo Italia

    98.7k15140238




    98.7k15140238












    • @val what made it legal?
      – opa
      Dec 20 at 20:28






    • 1




      The behavior of i = ++i + ++i is undefined in C++17
      – Jans
      Dec 20 at 20:32








    • 3




      @val: just checked again: it's not legal. C++17 just added a sequence point for assignment and compound assignment operators. stackoverflow.com/a/46171943/214671 ++i + ++i remains illegal even just by itself (without assignment on the left).
      – Matteo Italia
      Dec 20 at 20:33








    • 2




      @ruakh It's undefined because it cannot be defined. It carries too much ambiguity. The question "Why is it undefined" is because you can't define it.
      – Nelson
      Dec 21 at 5:46






    • 3




      @ruakh: ultimately, it's undefined because the standard says it's undefined; a good rationalization is that the standard left so much leeway for implementors to apply side-effects whenever and however they feel best that they went all the way and left it completely undefined to allow for any possible optimization (say, store half a value at some moment and the other half later, generating a trap representation in the meantime).
      – Matteo Italia
      Dec 21 at 6:59


















    • @val what made it legal?
      – opa
      Dec 20 at 20:28






    • 1




      The behavior of i = ++i + ++i is undefined in C++17
      – Jans
      Dec 20 at 20:32








    • 3




      @val: just checked again: it's not legal. C++17 just added a sequence point for assignment and compound assignment operators. stackoverflow.com/a/46171943/214671 ++i + ++i remains illegal even just by itself (without assignment on the left).
      – Matteo Italia
      Dec 20 at 20:33








    • 2




      @ruakh It's undefined because it cannot be defined. It carries too much ambiguity. The question "Why is it undefined" is because you can't define it.
      – Nelson
      Dec 21 at 5:46






    • 3




      @ruakh: ultimately, it's undefined because the standard says it's undefined; a good rationalization is that the standard left so much leeway for implementors to apply side-effects whenever and however they feel best that they went all the way and left it completely undefined to allow for any possible optimization (say, store half a value at some moment and the other half later, generating a trap representation in the meantime).
      – Matteo Italia
      Dec 21 at 6:59
















    @val what made it legal?
    – opa
    Dec 20 at 20:28




    @val what made it legal?
    – opa
    Dec 20 at 20:28




    1




    1




    The behavior of i = ++i + ++i is undefined in C++17
    – Jans
    Dec 20 at 20:32






    The behavior of i = ++i + ++i is undefined in C++17
    – Jans
    Dec 20 at 20:32






    3




    3




    @val: just checked again: it's not legal. C++17 just added a sequence point for assignment and compound assignment operators. stackoverflow.com/a/46171943/214671 ++i + ++i remains illegal even just by itself (without assignment on the left).
    – Matteo Italia
    Dec 20 at 20:33






    @val: just checked again: it's not legal. C++17 just added a sequence point for assignment and compound assignment operators. stackoverflow.com/a/46171943/214671 ++i + ++i remains illegal even just by itself (without assignment on the left).
    – Matteo Italia
    Dec 20 at 20:33






    2




    2




    @ruakh It's undefined because it cannot be defined. It carries too much ambiguity. The question "Why is it undefined" is because you can't define it.
    – Nelson
    Dec 21 at 5:46




    @ruakh It's undefined because it cannot be defined. It carries too much ambiguity. The question "Why is it undefined" is because you can't define it.
    – Nelson
    Dec 21 at 5:46




    3




    3




    @ruakh: ultimately, it's undefined because the standard says it's undefined; a good rationalization is that the standard left so much leeway for implementors to apply side-effects whenever and however they feel best that they went all the way and left it completely undefined to allow for any possible optimization (say, store half a value at some moment and the other half later, generating a trap representation in the meantime).
    – Matteo Italia
    Dec 21 at 6:59




    @ruakh: ultimately, it's undefined because the standard says it's undefined; a good rationalization is that the standard left so much leeway for implementors to apply side-effects whenever and however they feel best that they went all the way and left it completely undefined to allow for any possible optimization (say, store half a value at some moment and the other half later, generating a trap representation in the meantime).
    – Matteo Italia
    Dec 21 at 6:59













    20














    This



    ++*Ptr++;


    doesn't cause U.B and its evaluated as ++(*(Ptr++))





    • ptr++; /* address post incremented i.e doesn't change here itself */


    • *ptr; /* dereference same address i.e value at location where ptr earlier points i.e 6 */


    • ++*ptr; /* value changed where ptr points i.e Ar[0] becomes 7 */


    Note that post increments Ptr++ evaluated as





    • Ptr; /* Ptr doesn't change here itself in same expression */


    • Ptr = Ptr + 1; /* in next expression, Ptr considers the incremented one */






    share|improve this answer




























      20














      This



      ++*Ptr++;


      doesn't cause U.B and its evaluated as ++(*(Ptr++))





      • ptr++; /* address post incremented i.e doesn't change here itself */


      • *ptr; /* dereference same address i.e value at location where ptr earlier points i.e 6 */


      • ++*ptr; /* value changed where ptr points i.e Ar[0] becomes 7 */


      Note that post increments Ptr++ evaluated as





      • Ptr; /* Ptr doesn't change here itself in same expression */


      • Ptr = Ptr + 1; /* in next expression, Ptr considers the incremented one */






      share|improve this answer


























        20












        20








        20






        This



        ++*Ptr++;


        doesn't cause U.B and its evaluated as ++(*(Ptr++))





        • ptr++; /* address post incremented i.e doesn't change here itself */


        • *ptr; /* dereference same address i.e value at location where ptr earlier points i.e 6 */


        • ++*ptr; /* value changed where ptr points i.e Ar[0] becomes 7 */


        Note that post increments Ptr++ evaluated as





        • Ptr; /* Ptr doesn't change here itself in same expression */


        • Ptr = Ptr + 1; /* in next expression, Ptr considers the incremented one */






        share|improve this answer














        This



        ++*Ptr++;


        doesn't cause U.B and its evaluated as ++(*(Ptr++))





        • ptr++; /* address post incremented i.e doesn't change here itself */


        • *ptr; /* dereference same address i.e value at location where ptr earlier points i.e 6 */


        • ++*ptr; /* value changed where ptr points i.e Ar[0] becomes 7 */


        Note that post increments Ptr++ evaluated as





        • Ptr; /* Ptr doesn't change here itself in same expression */


        • Ptr = Ptr + 1; /* in next expression, Ptr considers the incremented one */







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Dec 20 at 19:01

























        answered Dec 20 at 18:44









        Achal

        8,8572728




        8,8572728






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53874307%2fis-ptr-undefined-behaviour-in-c%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Сан-Квентин

            8-я гвардейская общевойсковая армия

            Алькесар