Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6677625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:08:42+00:00 2026-05-26T04:08:42+00:00

We all know that dereferencing an null pointer or a pointer to unallocated memory

  • 0

We all know that dereferencing an null pointer or a pointer to unallocated memory invokes undefined behaviour.

But what is the rule when used within an expression passed to sizeof?

For example:

int *ptr = 0;
int size = sizeof(*ptr);

Is this also undefined?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T04:08:43+00:00Added an answer on May 26, 2026 at 4:08 am

    In most cases, you will find that sizeof(*x) does not actually evaluate *x at all. And, since it’s the evaluation (de-referencing) of a pointer that invokes undefined behaviour, you’ll find it’s mostly okay. The C11 standard has this to say in 6.5.3.4. The sizeof operator /2 (my emphasis in all these quotes):

    The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type. The size is determined from the type of the operand. The result is an integer. If the type of the operand is a variable length array type, the operand is evaluated; otherwise, the operand is not evaluated and the result is an integer constant.

    This is identical wording to the same section in C99. C89 had slightly different wording because, of course, there were no VLAs at that point. From 3.3.3.4. The sizeof operator:

    The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type. The size is determined from the type of the operand, which is not itself evaluated. The result is an integer constant.

    So, in C, for all non-VLAs, no dereferencing takes place and the statement is well defined. If the type of *x is a VLA, that’s considered an execution-phase sizeof, something that needs to be worked out while the code is running – all others can be calculated at compile time. If x itself is the VLA, it’s the same as the other cases, no evaluation takes place when using *x as an argument to sizeof().


    C++ has (as expected, since it’s a different language) slightly different rules, as shown in the various iterations of the standard:

    First, C++03 5.3.3. Sizeof /1:

    The sizeof operator yields the number of bytes in the object representation of its operand. The operand is either an expression, which is not evaluated, or a parenthesized type-id.

    In, C++11 5.3.3. Sizeof /1, you’ll find slightly different wording but the same effect:

    The sizeof operator yields the number of bytes in the object representation of its operand. The operand is either an expression, which is an unevaluated operand (Clause 5), or a parenthesized type-id.

    C++11 5. Expressions /7 (the above mentioned clause 5) defines the term “unevaluated operand” as perhaps one of the most useless, redundant phrases I’ve read for a while, but I don’t know what was going through the mind of the ISO people when they wrote it:

    In some contexts ([some references to sections detailing those contexts – pax]), unevaluated operands appear. An unevaluated operand is not evaluated.

    C++14/17 have the same wording as C++11 but not necessarily in the same sections, as stuff was added before the relevant parts. They’re in 5.3.3. Sizeof /1 and 5. Expressions /8 for C++14 and 8.3.3. Sizeof /1 and 8. Expressions /8 for C++17.

    So, in C++, evaluation of *x in sizeof(*x) never takes place, so it’s well defined, provided you follow all the other rules like providing a complete type, for example. But, the bottom line is that no dereferencing is done, which means it does not cause a problem.

    You can actually see this non-evaluation in the following program:

    #include <iostream>
    #include <cmath>
    
    int main() {
        int x = 42;
        std::cout << x << '\n';
    
        std::cout << sizeof(x = 6) << '\n';
        std::cout << sizeof(x++) << '\n';
        std::cout << sizeof(x = 15 * x * x + 7 * x - 12) << '\n';
        std::cout << sizeof(x += sqrt(4.0)) << '\n';
    
        std::cout << x << '\n';
    }
    

    You might think that the final line would output something vastly different to 42 (774, based on my rough calculations) because x has been changed quite a bit. But that is not actually the case since it’s only the type of the expression in sizeof that matters here, and the type boils down to whatever type x is.

    What you do see (other than the possibility of different pointer sizes on lines other than the first and last) is:

    42
    4
    4
    4
    4
    42
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

As we all know that iPod touch is having less memory than iPhone, but
We all know that this refers to the actual instance of a class...but it
We all know that 42 is the answer to everything , but it's news
We all know that global variables are anything but best practice. But there are
Possible Duplicates: Does the 'offsetof' macro from <stddef.h> invoke undefined behaviour? dereferencing the null
We all know that jagged array performs better than multi-dimensional array, but what about
We all know that Mathematica is great, but it also often lacks critical functionality.
so we all know that fancybox have resize functionality and thats great, but if
As we all know that within a network, communication is by the MAC address
We all know that RAW pointers need to be wrapped in some form of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.