指针和内存范围
我已经用 C 语言编程有一段时间了(但对 C 语言还是很陌生),有时我对 C 处理内存的方式感到困惑。
考虑以下有效的 C 代码片段:
const char *string(void)
{
/* where is this pointer variable located in the memory? */
const char *s;
/* where is this text data located in the memory? */
/* and when the program allocates memory for it? */
s = "Hello, World";
return s;
}
int main(void)
{
printf( "%s", string() );
return 0;
}
我问的是内存中究竟发生了什么? 指针变量不是局部变量还是指针变量存储在内存的什么位置。另外,文本常量“Hello,World”存储在内存中的位置(这不是被认为是函数返回后不可访问的局部变量)吗?
基本上什么样的变量/数据被认为是在函数的“本地”范围内(函数返回后不再可访问)?
我希望你明白我想说的:D..我想我有很多关于编译器和可执行文件的知识,所以请随意启发我!
I have been programming C for a while (but still pretty new to C) and I am sometimes getting confused of the way the C is handling the memory.
Consider following valid C snippet:
const char *string(void)
{
/* where is this pointer variable located in the memory? */
const char *s;
/* where is this text data located in the memory? */
/* and when the program allocates memory for it? */
s = "Hello, World";
return s;
}
int main(void)
{
printf( "%s", string() );
return 0;
}
I am asking what exactly is going on in the memory?
Isn't the pointer variable 's' a local variable or where is the pointer variable stored in the memory. Also where is the text constant "Hello, World" stored in memory (isn't this considered to be local variable which isnt accesible after function returns)?
Basically what kind of variables / data are consider to be in "local" scope of the functions (ceases to be accessible after function returns)?
I hope you understand what I am trying to say :D.. I think I have lot to learn about compilers and executables so feel free enlighten me!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我问的是内存中究竟发生了什么?
局部变量正在堆栈上分配。
常量(包括文字字符串)被分配在可执行文件的文本或数据部分中。
指针变量's'不是局部变量吗?
是
或者指针变量存储在内存的什么地方?
局部 >
s
位于寄存器或堆栈中。文本常量“Hello, World”存储在内存中的什么位置?
在.text 或.data 部分。它是不变的,但遗留代码有时会修改它们,因此它取决于编译器选项。您需要区分引用和对象才能理解这一切。
(这不是被认为是函数返回后不可访问的局部变量吗?
嗯,
s
是局部的,但每次函数调用时都需要字符串本身被调用,并且在发生之前本地框架甚至不会存在,因此常量本身很可能存储在 .text 部分中。它可能存储在 .data 中,具体取决于编译器选项以及当前编译器版本对编译遗留代码的关心程度。表达式内的文字与它所分配的变量完全不同。基本上什么类型的变量/数据被认为是在函数的“本地”范围内(函数返回后不再可访问)?
词法范围为
auto 变量,即在没有
static
存储类的函数内部声明的变量。不幸的是,“可访问”这个词有点不精确。使用静态存储类,如果对象的地址从函数中泄漏,则可以引用该对象。I am asking what exactly is going on in the memory?
Local variables are being allocated on the stack.
Constants, including the literal strings, are being allocated in the text or data sections of the executable.
Isn't the pointer variable 's' a local variable?
Yes
or where is the pointer variable stored in the memory?
The local
s
is in a register or on the stack.Also where is the text constant "Hello, World" stored in memory?
In either the .text or the .data section. It's constant, but legacy code would sometimes modify them, so it depends on compiler options. You need to distinguish between the reference and the object to understand it all.
(isn't this considered to be local variable which isn't accesible after function returns)?
Well,
s
is local but the string itself will be needed every time the function is called, and the local frame won't even exist until that happens, so the constant itself is most likely stored in the .text section. It may be stored in .data, depending on compiler options and how much the current compiler release cares about compiling legacy code. The literal inside the expression is something completely different from the variable it is being assigned to.Basically what kind of variables / data are consider to be in "local" scope of the functions (ceases to be accessible after function returns)?
The ones that are lexically scoped as
auto
variables, i.e., declared inside functions without thestatic
storage class. The wordaccessible
is unfortunately a bit imprecise. With static storage class the object could be referenced if its address was leaked out of the function.编译器已经准备好了字符串
“Hello, World”
的存储,这样在加载应用程序时它就有自己的内存地址。然后,当代码
s = "Hello, World";
运行时,您会将已编译字符串的内存地址复制到指针s
中。没有为字符串分配额外的内存,s
本身是一个局部变量(指针),暂时占用堆栈上的空间。The compiler has already prepared storage of the string
"Hello, World"
in such a way that it has its own memory address when your application is loaded.Then when your code
s = "Hello, World";
runs, you're copying the memory address of the compiled string into pointers
. No additional memory has been allocated for the string, ands
itself is a local variable (a pointer) temporarily taking up space on the stack.s
位于堆栈中,当string()
返回时不再存在。文字字符串存储在程序的只读数据中(这可能是也可能不是程序代码的一部分,具体取决于编译器和操作系统);使用gcc
,您可以通过使用-fwritable-strings
编译来使其成为读写初始化数据的一部分,但这是一个坏主意(它主要是为古代程序提供的)期望字符串文字是可写的)。s
is on the stack, and ceases to exist whenstring()
returns. The literal string is stored in the read-only data of the program (this may or may not be the part of the program's code, depending on the compiler and OS); withgcc
, you can make it part of the read-write initialized data by compiling with-fwritable-strings
, but this is a bad idea (it's mostly provided for ancient programs which expect string literals to be writable).s = "Hello, World";
s
是一个局部变量,而"Hello World"
是一个字符串文字,存储在内存的 const 区域中并具有静态存储时间。当函数返回时,s
被销毁,但字符串文字"Hello World"
却没有。s = "Hello, World";
s
is a local variable whereas"Hello World"
being a string literal is stored in const region of memory and has static storage duration.s
gets destroyed when the function returns but the string literal"Hello World"
doesn't.试试这个。可能工作
“const”是“不应该是左值”。
变量的“静态”是“静态分配”。
(您返回一个静态字符串)
变量 s 指向“静态分配”的区域
try this. may works
'const' is 'should not be left-valued'.
'static' for a variable ,is 'static allocated'.
(you're returning a static string)
varible s is point to an area which 'static allocated'