問題描述
c++ 中的字符串字面量是否在靜態內存中創建并僅在程序退出時銷毀?
Is a string literal in c++ created in static memory and destroyed only when the program exits?
推薦答案
它的創建位置實際上是編譯器編寫者的實現決定.最有可能的是,字符串文字將存儲在內存的只讀段中,因為它們永遠不會改變.
Where it's created is an implementation decision by the compiler writer, really. Most likely, string literals will be stored in read-only segments of memory since they never change.
在過去的編譯器時代,您曾經擁有像這些文字這樣的靜態數據,以及全局但可變的數據.這些被存儲在 TEXT(代碼)段和 DATA(初始化數據)段中.
In the old compiler days, you used to have static data like these literals, and global but changeable data. These were stored in the TEXT (code) segment and DATA (initialised data) segment.
即使你有像 char *x = "hello";
這樣的代碼,hello
字符串本身也存儲在只讀內存中,而變量 x
位于堆棧上(或者在可寫內存中的其他地方,如果它是全局的).x
只是設置為 hello
字符串的地址.這允許各種棘手的事情,例如字符串折疊,以便無效選項"(0x1000)和有效選項"(0x1002)可以使用相同的內存塊,如下所示:
Even when you have code like char *x = "hello";
, the hello
string itself is stored in read-only memory while the variable x
is on the stack (or elsewhere in writeable memory if it's a global). x
just gets set to the address of the hello
string. This allows all sorts of tricky things like string folding, so that "invalid option" (0x1000) and "valid option" (0x1002) can use the same memory block as follows:
+-> plus:0 1 2 3 4 5 6 7 8 9 A B C D E
| +---+---+---+---+---+---+---+---+---+---+---+---+---+---+----+
0x1000 | i | n | v | a | l | i | d | | o | p | t | i | o | n |
主站蜘蛛池模板:
午夜黄色小视频
|
午夜精品影院
|
av最新在线
|
97香蕉视频
|
天天曰天天操
|
欧美精品一二三区
|
久久成人毛片
|
国产黄色免费网站
|
黄色三级网
|
亚洲成人免费网站
|
欧美日韩一二区
|
欧美在线观看一区
|
午夜免费观看视频
|
日韩精品成人免费观看视频
|
国产成人在线免费视频
|
天天色网站
|
91av在线播放|
长河落日电视连续剧免费观看
|
国产精品久久网
|
午夜国产在线观看
|
欧美特黄视频
|
在线国产一区
|
亚洲精品久久
|
成人午夜毛片
|
日韩av不卡在线观看
|
亚洲国产精品久久
|
四虎入口
|
亚洲精品网站在线观看
|
国产精品久久久久久久
|
日韩网站在线观看
|
国产操操操
|
中文字幕免费在线看线人动作大片
|
国产99对白在线播放
|
九九精品视频在线观看
|
亚洲久草
|
成人小视频在线观看
|
一区二区国产精品
|
中文字字幕在线
|
青青草视频网站
|
成人夜色|
毛片tv
|