assigning string::c_str() to a const char* when the string goes out of scope

后端 未结 5 1700
北恋
北恋 2021-01-18 06:22

I have a doubt on basic C++ usage. The code below, compiled with gcc/LInux, prints out correctly.

The string test goes out of scope so also its c_

5条回答
  •  -上瘾入骨i
    2021-01-18 07:09

    Because test goes out of scope, what you are doing is undefined behavior. Do not make assumptions about how it will function.

    It could just as well segfault and the behavior will still be correct. UB is UB.

提交回复
热议问题