在内存中的字符串常量?栈/堆? [英] where in memory are string literals ? stack / heap?
问题描述
可能重复:结果
C字符串文字:?他们在哪里去
据我所知,
一般,指针必须
由malloc()分配,并且将分配
堆,然后通过免费的()未分配;
generally, pointer have to allocated by malloc(), and will allocated to heap, then unallocated by free();
和
非指针(INT,char和float等)会
自动分配堆栈,以及
未分配只要功能去
返回
non pointer(int,char,float,etc..) will allocated automatically to stack, and unallocated as long as the function go to return
不过,从以下code:
but, from following code :
#include <stdio.h>
int main()
{
char *a;
a = "tesaja";
return 0;
}
在这里将 A
分配?栈或堆?
推荐答案
的字符串文字会在数据段分配。该指针指向它, A
,将在栈上分配的。
The string literal will be allocated in data segment. The pointer to it, a
, will be allocated on the stack.
您code最终将被编译器转化成这样的:
Your code will eventually be transformed by the compiler into something like this:
#include <stdio.h>
const static char literal_constant_34562[7] = {'t', 'e', 's', 'a', 'j', 'a', '\0'};
int main()
{
char *a;
a = &literal_constant_34562[0];
return 0;
}
因此,确切的回答你的问题是:没有即可。 的Stack 的数据的 BSS 的和的堆的是内存中的所有不同的区域。量静态初始化的变量将在数据的
Therefore, the exact answer to your question is: neither. Stack, data, bss and heap are all different regions of memory. Const static initialized variables will be in data.
这篇关于在内存中的字符串常量?栈/堆?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!