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

前端 未结 6 1610
我在风中等你
我在风中等你 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:07

    Since sizeof does not evaluate its operand (except in the case of variable length arrays if you're using C99 or later), in the expression sizeof (*ptr), ptr is not evaluated, therefore it is not dereferenced. The sizeof operator only needs to determine the type of the expression *ptr to get the appropriate size.

提交回复
热议问题