Is “sizeof new int;” UB?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
7
down vote

favorite
1












code:



#include<iostream>

using namespace std;

int main()

size_t i = sizeof new int;

cout<<i;



In GCC compiler, working fine without any warning or error and printed output 8.



But, in clang compiler, i got following warning:



warning: expression with side effects has no effect in an unevaluated context [-Wunevaluated-expression]
size_t i = sizeof new int;


  • Which one is true?

  • Is sizeof new int; UB?









share|improve this question

















  • 1




    I see nothing wrong. This warning is more of a reminder, everything is well defined here.
    – YSC
    32 mins ago










  • This question is related to the C counterpart Why does sizeof(x++) not increment x?, but it is a bit clearer in C++.
    – Bartek Banachewicz
    18 mins ago











  • @BartekBanachewicz - a closer equivalent in C would be sizeof malloc(some_value) which would not call malloc() and, if performed after #include <stdlib.h>, would give a result equal to sizeof(void *).
    – Peter
    11 mins ago















up vote
7
down vote

favorite
1












code:



#include<iostream>

using namespace std;

int main()

size_t i = sizeof new int;

cout<<i;



In GCC compiler, working fine without any warning or error and printed output 8.



But, in clang compiler, i got following warning:



warning: expression with side effects has no effect in an unevaluated context [-Wunevaluated-expression]
size_t i = sizeof new int;


  • Which one is true?

  • Is sizeof new int; UB?









share|improve this question

















  • 1




    I see nothing wrong. This warning is more of a reminder, everything is well defined here.
    – YSC
    32 mins ago










  • This question is related to the C counterpart Why does sizeof(x++) not increment x?, but it is a bit clearer in C++.
    – Bartek Banachewicz
    18 mins ago











  • @BartekBanachewicz - a closer equivalent in C would be sizeof malloc(some_value) which would not call malloc() and, if performed after #include <stdlib.h>, would give a result equal to sizeof(void *).
    – Peter
    11 mins ago













up vote
7
down vote

favorite
1









up vote
7
down vote

favorite
1






1





code:



#include<iostream>

using namespace std;

int main()

size_t i = sizeof new int;

cout<<i;



In GCC compiler, working fine without any warning or error and printed output 8.



But, in clang compiler, i got following warning:



warning: expression with side effects has no effect in an unevaluated context [-Wunevaluated-expression]
size_t i = sizeof new int;


  • Which one is true?

  • Is sizeof new int; UB?









share|improve this question













code:



#include<iostream>

using namespace std;

int main()

size_t i = sizeof new int;

cout<<i;



In GCC compiler, working fine without any warning or error and printed output 8.



But, in clang compiler, i got following warning:



warning: expression with side effects has no effect in an unevaluated context [-Wunevaluated-expression]
size_t i = sizeof new int;


  • Which one is true?

  • Is sizeof new int; UB?






c++ c++11 g++ sizeof clang++






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 33 mins ago









M.S Chaudhari

2,65211030




2,65211030







  • 1




    I see nothing wrong. This warning is more of a reminder, everything is well defined here.
    – YSC
    32 mins ago










  • This question is related to the C counterpart Why does sizeof(x++) not increment x?, but it is a bit clearer in C++.
    – Bartek Banachewicz
    18 mins ago











  • @BartekBanachewicz - a closer equivalent in C would be sizeof malloc(some_value) which would not call malloc() and, if performed after #include <stdlib.h>, would give a result equal to sizeof(void *).
    – Peter
    11 mins ago













  • 1




    I see nothing wrong. This warning is more of a reminder, everything is well defined here.
    – YSC
    32 mins ago










  • This question is related to the C counterpart Why does sizeof(x++) not increment x?, but it is a bit clearer in C++.
    – Bartek Banachewicz
    18 mins ago











  • @BartekBanachewicz - a closer equivalent in C would be sizeof malloc(some_value) which would not call malloc() and, if performed after #include <stdlib.h>, would give a result equal to sizeof(void *).
    – Peter
    11 mins ago








1




1




I see nothing wrong. This warning is more of a reminder, everything is well defined here.
– YSC
32 mins ago




I see nothing wrong. This warning is more of a reminder, everything is well defined here.
– YSC
32 mins ago












This question is related to the C counterpart Why does sizeof(x++) not increment x?, but it is a bit clearer in C++.
– Bartek Banachewicz
18 mins ago





This question is related to the C counterpart Why does sizeof(x++) not increment x?, but it is a bit clearer in C++.
– Bartek Banachewicz
18 mins ago













@BartekBanachewicz - a closer equivalent in C would be sizeof malloc(some_value) which would not call malloc() and, if performed after #include <stdlib.h>, would give a result equal to sizeof(void *).
– Peter
11 mins ago





@BartekBanachewicz - a closer equivalent in C would be sizeof malloc(some_value) which would not call malloc() and, if performed after #include <stdlib.h>, would give a result equal to sizeof(void *).
– Peter
11 mins ago













2 Answers
2






active

oldest

votes

















up vote
15
down vote



accepted










The warning doesn't state that it's UB; it merely says that the context of use, namely sizeof, won't trigger the side effects (which in case of new is allocating memory).




[expr.sizeof]
The sizeof operator yields the number of bytes occupied by a non-potentially-overlapping object of the type of its operand. The operand is either an expression, which is an unevaluated operand ([expr.prop]), or a parenthesized type-id.




The standard also helpfully explains what that means:




[expr.context] (...) An unevaluated operand is not evaluated.




It's a fine, although a weird way to write sizeof(int*).






share|improve this answer



























    up vote
    4
    down vote













    new operator returns pointer to the allocated memory. So size_t i = sizeof new int; will not cause undefined behaviour.

    Warning is legit and only warns about the effect of side-effect on the operand and that's because operands of sizeof is not evaluated.



    For example:



    int i = 1;
    std::cout << i << 'n'; // Prints 1
    size_t size = sizeof(i++); // i++ will not be evaluated
    std::cout << i << 'n'; // Prints 1





    share|improve this answer






















    • Curiously, it's easy to make i++ be evaluated: cpp.sh/9774o
      – Dan M.
      9 mins ago










    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',
    convertImagesToLinks: true,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    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%2f52624526%2fis-sizeof-new-int-ub%23new-answer', 'question_page');

    );

    Post as a guest






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    15
    down vote



    accepted










    The warning doesn't state that it's UB; it merely says that the context of use, namely sizeof, won't trigger the side effects (which in case of new is allocating memory).




    [expr.sizeof]
    The sizeof operator yields the number of bytes occupied by a non-potentially-overlapping object of the type of its operand. The operand is either an expression, which is an unevaluated operand ([expr.prop]), or a parenthesized type-id.




    The standard also helpfully explains what that means:




    [expr.context] (...) An unevaluated operand is not evaluated.




    It's a fine, although a weird way to write sizeof(int*).






    share|improve this answer
























      up vote
      15
      down vote



      accepted










      The warning doesn't state that it's UB; it merely says that the context of use, namely sizeof, won't trigger the side effects (which in case of new is allocating memory).




      [expr.sizeof]
      The sizeof operator yields the number of bytes occupied by a non-potentially-overlapping object of the type of its operand. The operand is either an expression, which is an unevaluated operand ([expr.prop]), or a parenthesized type-id.




      The standard also helpfully explains what that means:




      [expr.context] (...) An unevaluated operand is not evaluated.




      It's a fine, although a weird way to write sizeof(int*).






      share|improve this answer






















        up vote
        15
        down vote



        accepted







        up vote
        15
        down vote



        accepted






        The warning doesn't state that it's UB; it merely says that the context of use, namely sizeof, won't trigger the side effects (which in case of new is allocating memory).




        [expr.sizeof]
        The sizeof operator yields the number of bytes occupied by a non-potentially-overlapping object of the type of its operand. The operand is either an expression, which is an unevaluated operand ([expr.prop]), or a parenthesized type-id.




        The standard also helpfully explains what that means:




        [expr.context] (...) An unevaluated operand is not evaluated.




        It's a fine, although a weird way to write sizeof(int*).






        share|improve this answer












        The warning doesn't state that it's UB; it merely says that the context of use, namely sizeof, won't trigger the side effects (which in case of new is allocating memory).




        [expr.sizeof]
        The sizeof operator yields the number of bytes occupied by a non-potentially-overlapping object of the type of its operand. The operand is either an expression, which is an unevaluated operand ([expr.prop]), or a parenthesized type-id.




        The standard also helpfully explains what that means:




        [expr.context] (...) An unevaluated operand is not evaluated.




        It's a fine, although a weird way to write sizeof(int*).







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 31 mins ago









        Bartek Banachewicz

        28.8k460103




        28.8k460103






















            up vote
            4
            down vote













            new operator returns pointer to the allocated memory. So size_t i = sizeof new int; will not cause undefined behaviour.

            Warning is legit and only warns about the effect of side-effect on the operand and that's because operands of sizeof is not evaluated.



            For example:



            int i = 1;
            std::cout << i << 'n'; // Prints 1
            size_t size = sizeof(i++); // i++ will not be evaluated
            std::cout << i << 'n'; // Prints 1





            share|improve this answer






















            • Curiously, it's easy to make i++ be evaluated: cpp.sh/9774o
              – Dan M.
              9 mins ago














            up vote
            4
            down vote













            new operator returns pointer to the allocated memory. So size_t i = sizeof new int; will not cause undefined behaviour.

            Warning is legit and only warns about the effect of side-effect on the operand and that's because operands of sizeof is not evaluated.



            For example:



            int i = 1;
            std::cout << i << 'n'; // Prints 1
            size_t size = sizeof(i++); // i++ will not be evaluated
            std::cout << i << 'n'; // Prints 1





            share|improve this answer






















            • Curiously, it's easy to make i++ be evaluated: cpp.sh/9774o
              – Dan M.
              9 mins ago












            up vote
            4
            down vote










            up vote
            4
            down vote









            new operator returns pointer to the allocated memory. So size_t i = sizeof new int; will not cause undefined behaviour.

            Warning is legit and only warns about the effect of side-effect on the operand and that's because operands of sizeof is not evaluated.



            For example:



            int i = 1;
            std::cout << i << 'n'; // Prints 1
            size_t size = sizeof(i++); // i++ will not be evaluated
            std::cout << i << 'n'; // Prints 1





            share|improve this answer














            new operator returns pointer to the allocated memory. So size_t i = sizeof new int; will not cause undefined behaviour.

            Warning is legit and only warns about the effect of side-effect on the operand and that's because operands of sizeof is not evaluated.



            For example:



            int i = 1;
            std::cout << i << 'n'; // Prints 1
            size_t size = sizeof(i++); // i++ will not be evaluated
            std::cout << i << 'n'; // Prints 1






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 25 mins ago

























            answered 31 mins ago









            haccks

            84.2k20123215




            84.2k20123215











            • Curiously, it's easy to make i++ be evaluated: cpp.sh/9774o
              – Dan M.
              9 mins ago
















            • Curiously, it's easy to make i++ be evaluated: cpp.sh/9774o
              – Dan M.
              9 mins ago















            Curiously, it's easy to make i++ be evaluated: cpp.sh/9774o
            – Dan M.
            9 mins ago




            Curiously, it's easy to make i++ be evaluated: cpp.sh/9774o
            – Dan M.
            9 mins ago

















             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52624526%2fis-sizeof-new-int-ub%23new-answer', 'question_page');

            );

            Post as a guest













































































            Comments

            Popular posts from this blog

            What does second last employer means? [closed]

            List of Gilmore Girls characters

            Confectionery