例如,在C中定义变量时,存储该变量的内存地址在哪里?

时间:2022-09-11 19:52:23

If I define a variable in C (i.e. unsigned short int n = 5), that value is stored somewhere in the user's RAM in binary (in this case it would look something like 0000 0000 0000 0101). The place in which that value is stored has an address that is also in binary. (i.e. the value 5 could be stored at say 0010 which would mean it uses both 0010 and 0011 in ram since it uses 2 bytes). The name of the variable n represents the memory address where that value is stored. Where is that memory address stored? Wouldn't that take up even more ram? If it did then wouldn't that address have to have an address as well?

如果我在C中定义一个变量(即unsigned short int n = 5),那么该值以二进制形式存储在用户RAM的某处(在这种情况下,它看起来像0000 0000 0000 0101)。存储该值的位置具有也是二进制的地址。 (即,值5可以存储在例如0010处,这意味着它在ram中使用0010和0011,因为它使用2个字节)。变量n的名称表示存储该值的内存地址。存储的内存地址在哪里?那会不会占用更多的公羊?如果确实如此,那么该地址也不必有地址吗?

2 个解决方案

#1


21  

The memory address of a variable is not stored directly in memory. It is part of the code that accesses the variable. Depending on the exact circumstances, it's either an offset (distance from a known place - for example the stack pointer for a local variable, and for a global variable it may be the program counter) or an absolute address (only for global variables).

变量的内存地址不直接存储在内存中。它是访问变量的代码的一部分。根据具体情况,它可以是偏移量(距离已知位置的距离 - 例如局部变量的堆栈指针,全局变量可能是程序计数器)或绝对地址(仅适用于全局变量)。

If you want a variable to store the address of a variable, then yes, you need memory for that variable too. This type of variable is called a pointer.

如果您希望变量存储变量的地址,那么是的,您也需要该变量的内存。这种类型的变量称为指针。

#2


1  

It depends on several factors, such as the allocation method (stack or static), how the variable is accessed, but let's assume this piece of code:

它取决于几个因素,例如分配方法(堆栈或静态),如何访问变量,但让我们假设这段代码:

static int n = 5;
printf("%p\n", &n);

In this case, the address of n is stored in the code segment, where printf is called. If you disassemble the code, you'll find a push instruction, pushing an address to the stack, right before calling printf. The address being pushed is the address of n (it's one of two addresses being pushed, there's also the format string).

在这种情况下,n的地址存储在代码段中,其中调用printf。如果您反汇编代码,您将在调用printf之前找到推送指令,将地址推送到堆栈。被推送的地址是n的地址(它是被推送的两个地址之一,也有格式字符串)。

As I said above, it isn't always the same way. Different architectures and compilation flags (e.g. -fpic) can change it.
Also, if the variable is on the stack, or if the reference to it is not from code, but from data (e.g. int n=5; int *p = &n;), things change.

正如我上面所说,它并不总是一样的。不同的体系结构和编译标志(例如-fpic)可以改变它。此外,如果变量在堆栈上,或者对它的引用不是来自代码,而是来自数据(例如int n = 5; int * p =&n;),则事情会发生变化。

#1


21  

The memory address of a variable is not stored directly in memory. It is part of the code that accesses the variable. Depending on the exact circumstances, it's either an offset (distance from a known place - for example the stack pointer for a local variable, and for a global variable it may be the program counter) or an absolute address (only for global variables).

变量的内存地址不直接存储在内存中。它是访问变量的代码的一部分。根据具体情况,它可以是偏移量(距离已知位置的距离 - 例如局部变量的堆栈指针,全局变量可能是程序计数器)或绝对地址(仅适用于全局变量)。

If you want a variable to store the address of a variable, then yes, you need memory for that variable too. This type of variable is called a pointer.

如果您希望变量存储变量的地址,那么是的,您也需要该变量的内存。这种类型的变量称为指针。

#2


1  

It depends on several factors, such as the allocation method (stack or static), how the variable is accessed, but let's assume this piece of code:

它取决于几个因素,例如分配方法(堆栈或静态),如何访问变量,但让我们假设这段代码:

static int n = 5;
printf("%p\n", &n);

In this case, the address of n is stored in the code segment, where printf is called. If you disassemble the code, you'll find a push instruction, pushing an address to the stack, right before calling printf. The address being pushed is the address of n (it's one of two addresses being pushed, there's also the format string).

在这种情况下,n的地址存储在代码段中,其中调用printf。如果您反汇编代码,您将在调用printf之前找到推送指令,将地址推送到堆栈。被推送的地址是n的地址(它是被推送的两个地址之一,也有格式字符串)。

As I said above, it isn't always the same way. Different architectures and compilation flags (e.g. -fpic) can change it.
Also, if the variable is on the stack, or if the reference to it is not from code, but from data (e.g. int n=5; int *p = &n;), things change.

正如我上面所说,它并不总是一样的。不同的体系结构和编译标志(例如-fpic)可以改变它。此外,如果变量在堆栈上,或者对它的引用不是来自代码,而是来自数据(例如int n = 5; int * p =&n;),则事情会发生变化。