Is sizeof(*ptr) undefined behavior when pointing to invalid memory?

前端 未结 6 1606
我在风中等你
我在风中等你 2020-11-30 04:21

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 pa

6条回答
  •  甜味超标
    2020-11-30 05:04

    The answer may well be different for C, where sizeof is not necessarily a compile-time construct, but in C++ the expression provided to sizeof is never evaluated. As such, there is never a possibility for undefined behavior to exhibit itself. By similar logic, you can also "call" functions that are never defined [because the function is never actually called, no definition is necessary], a fact that is frequently used in SFINAE rules.

提交回复
热议问题