指针和内存范围 [英] Pointers and memory scope

查看:81
本文介绍了指针和内存范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经进行了一段时间的C编程(但是对C还是很陌生),有时我对C处理内存的方式感到困惑.

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.

考虑以下有效的C代码段:

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;
}

我要问的是存储器中到底发生了什么? 指针变量不是本地变量,还是存储在内存中的指针变量在哪里?文本常量"Hello,World"还存储在内存中的什么位置(这不认为是在函数返回后不可访问的局部变量)吗?

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)?

希望您能理解我想说的:D ..我想我对编译器和可执行文件有很多了解,所以请随时赐教!

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!

推荐答案

我问的是内存中到底发生了什么?

局部变量正在堆栈中分配. 常量(包括文字字符串)将分配到可执行文件的文本或数据部分.

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?

还是指针变量存储在内存中?

本地 s 在寄存器中或堆栈中.

The local s is in a register or on the stack.

文本常量"Hello,World" 存储在内存中的什么地方?

Also where is the text constant "Hello, World" stored in memory?

在.text或.data节中.它是恒定的,但是旧版代码有时会对其进行修改,因此它取决于编译器选项.您需要区分引用和对象以了解所有内容.

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.

(这是否被认为是在函数返回后不可访问的局部变量?)?

好吧,s是局部的,但是每次调用该函数时都需要字符串本身,并且局部帧甚至在这种情况发生之前都不会存在,因此常量本身很可能存储在.text节中.它可能存储在.data中,具体取决于编译器选项以及当前编译器版本对编译遗留代码的关心程度.表达式中的文字与为其分配的变量完全不同.

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.

基本上是什么类型的变量/数据被认为在函数的本地"范围内(在函数返回后可以访问)?

在词法上限定为auto变量的变量,即在没有static存储类的函数内部声明的变量.不幸的是,单词accessible有点不精确.对于静态存储类,如果对象的地址从函数中泄漏出来,则可以引用该对象.

The ones that are lexically scoped as auto variables, i.e., declared inside functions without the static storage class. The word accessible is unfortunately a bit imprecise. With static storage class the object could be referenced if its address was leaked out of the function.

这篇关于指针和内存范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆