Is %p specifier only for valid pointers?

前端 未结 4 1402
失恋的感觉
失恋的感觉 2021-02-12 12:47

Suppose on my platform sizeof(int)==sizeof(void*) and I have this code:

printf( \"%p\", rand() );

Will this be undefined behavior

4条回答
  •  天涯浪人
    2021-02-12 13:41

    Yes, it's undefined. From C++11, 3.7.4.2/4:

    The effect of using an invalid pointer value (including passing it to a deallocation function) is undefined.

    with a footnote:

    On some implementations, it causes a system-generated runtime fault.

提交回复
热议问题