※ 引述《jenniskuo.bbs@ptt.cc (冷暖)》之銘言:
> ※ 引述《swli.bbs@bbs.ntu.edu.tw (我喜歡寫程式)》之銘言:
> 不知道我這樣想對不對,
> 根據你所說的,
> char *str = "string"; 這個敘述代表 "str變數" 放在 stack 中,
> 而 "string" 本身是放在 heap 中.
> char str[] = "string"; 這個敘述代表 "str常數" 放在常數區段,
> 而 "string" 本身是放在 stack 中.
> 是嗎 ?
"xxxxxx" 都是放在 constant pool 中,
而且通常 compiler 會將 constant pool 設為 read only。
char *ptr = "xxxxxx"; 是讓 ptr 指向 constant pool 裡第一個 'x' 的位置。
char str[] = "xxxxxx"; 是在 stack 中開 7 個 bytes 的空間,
並將 'x' 'x' 'x' 'x' 'x' 'x' '\0' 七個字元從 constant pool 中複製過來。
--
Name: Tseng, Ling-hua E-mail Address: uranus@it.muds.net
School: National Tsing Hua University Department: Computer Science
Interesting: C++, Compiler, PL/PD, OS, VM, Large-scale software design
Researching: Undecided
Homepage: https://it.muds.net/~uranus
--
╔═══╗ ┼────────────────────────╮
║狂狷 ║ │* Origin:[ 狂 狷 年 少 ] whshs.cs.nccu.edu.tw ╰─╮
║ 年少║ ┼╮ < IP:140.119.164.252 > ╰─╮
╚╦═╦╝ ╰ * From:61-230-216-250.dynamic.hinet.net
─╨─╨─ KGBBS ─ ◎ 遨翔"BBS"的狂狷不馴;屬於年少的輕狂色彩 ◎