Is a string literal in c++ created in static memory?

前端 未结 4 2118
情书的邮戳
情书的邮戳 2020-11-22 15:07

Is a string literal in c++ created in static memory and destroyed only when the program exits?

4条回答
  •  粉色の甜心
    2020-11-22 16:04

    Well ... Yes. They sort of have to be; the information that makes up the sequence of characters in each string has to be somewhere. If they were to be allocated dynamically and then initialized, where would the information used to to the initialization reside? Thus, it's more efficient to simply make the strings static, so that they are always available and valid once the program is done loading.

提交回复
热议问题