l**********r 发帖数: 4612 | 1 【 以下文字转载自 Programming 讨论区 】
发信人: linuxbeginer (linux), 信区: Programming
标 题: Char x[] = "abc"; 是在heap还是stack上?
发信站: BBS 未名空间站 (Mon Oct 19 17:15:12 2009, 美东)
Char x[] = "abc";
我认为内存allocated 在heap上。对么?
suppose char x[] is defined at global scope. |
t**e 发帖数: 208 | 2 no, x is on stack. "abc" is in global data segment.
【在 l**********r 的大作中提到】 : 【 以下文字转载自 Programming 讨论区 】 : 发信人: linuxbeginer (linux), 信区: Programming : 标 题: Char x[] = "abc"; 是在heap还是stack上? : 发信站: BBS 未名空间站 (Mon Oct 19 17:15:12 2009, 美东) : Char x[] = "abc"; : 我认为内存allocated 在heap上。对么? : suppose char x[] is defined at global scope.
|
l**********r 发帖数: 4612 | 3 suppose char x[] is defined at global scope.
I think x[] is on heap with 4 bytes memory. Compiler allocated the space and
copied "abc" on the space |
s****g 发帖数: 56 | 4 4 bytes at least. There is a '\0'.
I think heap memory is allocated with malloc.
Otherwise on stack or somewhere else.
Probably x is on stack (it is a pointer) and "abc" is on
global constant area. Not 100% sure.
space and
【在 l**********r 的大作中提到】 : suppose char x[] is defined at global scope. : I think x[] is on heap with 4 bytes memory. Compiler allocated the space and : copied "abc" on the space
|
l**********r 发帖数: 4612 | 5 If it's
char * x = "abc";
then you are right. But for
char x[] = "abc";
I think you are wrong.
Anybody can confirm with that?
【在 t**e 的大作中提到】 : no, x is on stack. "abc" is in global data segment.
|
l**********r 发帖数: 4612 | 6 yes, sorry for the typo.
【在 s****g 的大作中提到】 : 4 bytes at least. There is a '\0'. : I think heap memory is allocated with malloc. : Otherwise on stack or somewhere else. : Probably x is on stack (it is a pointer) and "abc" is on : global constant area. Not 100% sure. : : space and
|
t**e 发帖数: 208 | 7 sorry. Y, I think you are right. all on stack.
【在 l**********r 的大作中提到】 : If it's : char * x = "abc"; : then you are right. But for : char x[] = "abc"; : I think you are wrong. : Anybody can confirm with that?
|
l**********r 发帖数: 4612 | 8 Hmmmm. what I mean is for
Char x[] = "abc"
the "abc" that x points to is on *heap*
【在 t**e 的大作中提到】 : sorry. Y, I think you are right. all on stack.
|
t****t 发帖数: 6806 | 9 你mean得不对
另外, 请正确使用大小写, 如果你说的是C/C++
【在 l**********r 的大作中提到】 : Hmmmm. what I mean is for : Char x[] = "abc" : the "abc" that x points to is on *heap*
|
l**********r 发帖数: 4612 | 10 char * x = "abc"; 这是个literal,一般情况下都在data segment里吧? |
y*****a 发帖数: 171 | 11 how could it be in the heap, you never call alloc for this, do you. it is
done by the compiler. you got it clear?
l.c
char *x1="abc1";
char x2[]="abc2";
gcc -s -c l.c
cat l.s
.file "l.c"
.globl x1
.section .rodata
.LC0:
.string "abc1"
.data
.align 4
.type x1, @object
.size x1, 4
x1:
.long .LC0
.globl x2
.type x2, @object
.size x2, 5
x2:
.string "abc2"
.ident "GCC: (GNU) 4.1.2 2008 |
j*****j 发帖数: 115 | 12 对 是在data segment上的,说错了,汗~~~
【在 l**********r 的大作中提到】 : char * x = "abc"; 这是个literal,一般情况下都在data segment里吧?
|