問題描述
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在线
|
欧美黄色一区二区
|
免费的黄色小视频
|
国产精品99999
|
国产成人午夜高潮毛片
|
日韩午夜在线观看
|
狠狠的日
|
国产天堂av
|
91日韩在线|
欧美日韩综合
|
毛片毛片毛片毛片毛片
|
日韩伦理在线观看
|
狠狠躁日日躁夜夜躁2022麻豆
|
亚洲欧美日韩国产精品
|
中文字幕2021
|
欧美激情综合网
|
中文字幕高清在线
|
亚洲一区在线播放
|
久久xx|
亚洲 欧美 日韩 在线
|
成人精品国产
|
一级片网址
|
视频一二三区
|
日本综合久久
|
天天干夜夜爱
|
人人射人人|
双性呜呜宫交受不住了h
|
黄片毛片在线观看
|
国产成人在线观看免费网站
|
亚洲综合自拍
|
在线a
|
午夜小视频在线观看
|
欧美福利一区
|
懂色av色吟av夜夜嗨
|
影音先锋国产精品
|