刚刚在gdb中检查了以下内容:
char *a[] = {"one","two","three","four"};
char *b[] = {"one","two","three","four"};
char *c[] = {"two","three","four","five"};
char *d[] = {"one","three","four","six"};
我得到以下内容:
(gdb) p a
$17 = {0x80961a4 "one", 0x80961a8 "two", 0x80961ac "three", 0x80961b2 "four"}
(gdb) p b
$18 = {0x80961a4 "one", 0x80961a8 "two", 0x80961ac "three", 0x80961b2 "four"}
(gdb) p c
$19 = {0x80961a8 "two", 0x80961ac "three", 0x80961b2 "four", 0x80961b7 "five"}
(gdb) p d
$20 = {0x80961a4 "one", 0x80961ac "three", 0x80961b2 "four", 0x80961bc "six"}
等效词的字符串指针是相同的,我感到非常惊讶。我本以为每个字符串都会在堆栈上分配自己的内存,而不管它是否与另一个数组中的字符串相同。
这是某种编译器优化的示例,还是这种字符串声明的标准行为?
慕无忌1623718
倚天杖
阿波罗的战车
相关分类