有关C++中字符串字面值的数据类型已经有类似的问题被问及。
许多人引用了标准:
窄字符串字面值的类型为“n个const char的数组”,其中n是如下所定义的字符串的大小,并具有静态存储期(3.7)。
我在主函数中编写了以下语句:
char cstring[]= "hellohellohellohellohellohello";
但是我在程序集中找不到任何以静态数据形式存储的字符串字面量。实际上,程序集显示该字符串被分解并直接 "存储" 在指令中。
movl $1819043176, -48(%rbp)
movl $1818585199, -44(%rbp)
movl $1701343084, -40(%rbp)
movl $1752132716, -36(%rbp)
movl $1869376613, -32(%rbp)
movl $1819043176, -28(%rbp)
movl $1818585199, -24(%rbp)
movw $28524, -20(%rbp)
movb $0, -18(%rbp)
当在全局作用域中有类似的语句时,其结果是将字符串存储为静态数据。
char cstring1[] = "hellohellohellohellohellohello";
汇编语言
cstring1:
.string "hellohellohellohellohellohello"
上述示例可以在此处在线查看:这里。
因此,这似乎不符合引用标准。也许有一些例外情况没有被引用到?
char const cstring[]= "hellohellohellohellohellohello";
- Richard Critten