由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - Char x[] = "abc"; 是在heap还是stack上? (转载)
相关主题
A problem about Heap and Stack.请教个C++编程思路
问个C++题为什么C++的constructor出错可以抛出异常,而destructor出错
Bloomberg面试题 兼问在纽约工作舒服吗?A malloc/free question using C/C++
Windows下多个DLL之间memory allocation问题 (转载)请教几个面试问题
C++ Q66: reverse a string -- is it efficienthow to solve this google interview question
c++ grill - how to dynamically allocate memory on stack?heap 和 stock都有些啥区别啊?
问个C++ delete[]问题问一个关于c++的很傻的问题,多谢
问一个C++ set和unordered_set iterator的问题问一个memory allocate/release的问题
相关话题的讨论汇总
话题: char话题: abc话题: heap话题: stack话题: x2
进入JobHunting版参与讨论
1 (共1页)
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里吧?
1 (共1页)
进入JobHunting版参与讨论
相关主题
问一个memory allocate/release的问题C++ Q66: reverse a string -- is it efficient
Thread safe strcpy ??c++ grill - how to dynamically allocate memory on stack?
贡献几个 c 电面问题问个C++ delete[]问题
a small question about c++ object allocation问一个C++ set和unordered_set iterator的问题
A problem about Heap and Stack.请教个C++编程思路
问个C++题为什么C++的constructor出错可以抛出异常,而destructor出错
Bloomberg面试题 兼问在纽约工作舒服吗?A malloc/free question using C/C++
Windows下多个DLL之间memory allocation问题 (转载)请教几个面试问题
相关话题的讨论汇总
话题: char话题: abc话题: heap话题: stack话题: x2