Is using NULL references OK?

前端 未结 7 1407
一整个雨季
一整个雨季 2020-12-05 20:37

I came across this code:

void f(const std::string &s);

And then a call:

f( *((std::string*)NULL) );

A

7条回答
  •  离开以前
    2020-12-05 21:09

    f( *((std::string*)NULL) );
    

    This is essentially dereferencing NULL, which on most systems is #defined to be 0. Last I checked 0x00000000 is an invalid memory address for doing anything.

    Whatever happened to just checking

    if (std::string.length() > 0) ....
    

提交回复
热议问题