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

后端 未结 5 1703
北恋
北恋 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条回答
  •  甜味超标
    2021-01-18 06:57

    It could be because of string pooling. Neverthless, it is Undefined behavior. We should not use the c_str() output once the string goes out of scope.

提交回复
热议问题