Why doesn't my program seg fault when I dereference a NULL pointer inside of malloc?

后端 未结 4 849
灰色年华
灰色年华 2020-12-03 18:28

I use this malloc style all the time

int *rc = 0;
rc = malloc(sizeof(*rc));

However, it doesn\'t seg fault even though when I call

4条回答
  •  無奈伤痛
    2020-12-03 18:42

    The sizeof operator doesn't actually evaluate its operand, it only looks at its type. The type of *rc is int, so it's equivalent to sizeof (int). This all happens at compile time.

    (Also, this is not "inside of malloc".)

提交回复
热议问题