【发布时间】:2011-12-03 04:41:09
【问题描述】:
#include<stdio.h>
int main(void)
{
/* in this amessage is an array big enough to
* hold the sequence of character and '\0' */
char amessage[] = "now is the time";
/* in this case a character pointer is
* pointing to a string constant */
char *pmessage = "now is the time";
return 0;
}
我很困惑在第二种情况下如何分配内存?那个内存副本是私有的吗……我的意思是写一些东西作为 2nd stmt 是否安全,并确保其他任何东西都不会覆盖相同的内存??
【问题讨论】:
-
不,你不能确定。你应该通过你的编程来照顾自己
-
@Mr.DDD:你确定吗?我认为大多数编译器会将这些数据加载到进程内存的不同段中,而不是堆中。
-
@Mr.DDD - 不会有任何加载(复制)。它很可能会指向可执行文件数据部分中的某个位置
-
使用带有默认优化选项的microsoft c,字符串数据本身被分配在静态数据段中。在这两种情况下,堆栈上都没有分配任何内容。指向数据段的指针在寄存器中分配。任何一个字符串都是可变的。
标签: c string memory pointers memory-management