关于 C 中变量内存地址的观察

发布于 2024-10-06 20:45:29 字数 1460 浏览 0 评论 0 原文

我有一个问题而不是问题(女巫可能会出现内存问题)..我编写了这个简单的程序:

#include <stdio.h>
#include <stdlib.h>

int multi(int x, int y);


int main(){
    int x;
    int y;
    printf("Enter the first number x: \n");
    scanf("%d",&x);
    printf("Enter the second number y: \n");
    scanf("%d",&y);
    int z=multi(x,y);
    printf("The Result of the multiplication is : %d\n",z,"\n");
    printf("The Memory adresse of x is : %d\n",&x);
    printf("The Memory adresse of y is : %d\n",&y);
    printf("The Memory adresse of z is : %d\n",&z);
    getchar();
    return 0;
}

int multi(int x,int y){
    int c=x*y;
    printf("The Memory adresse of c is : %d\n",&c);
    return c;  
}

如您所见(如果您用 C 语言开发),该程序输入 2 个 int 变量,然后将它们与多功能:

得到结果后,显示每个变量在内存中的位置(cxyz )。

我已经测试了这个简单的示例,这些是结果(在我的例子中):

The Memory adresse of c is : 2293556  
The Result of the multiplication is : 12  
The Memory adresse of x is : 2293620  
The Memory adresse of y is : 2293616  
The Memory adresse of z is : 2293612  

如您所见,三个变量 x,y,z 在 main 函数中声明的变量具有封闭的内存地址 (22936xx) ,在 multi 函数中声明的变量 c 具有不同的地址 (22935xx)。

查看 xyz 变量,我们可以看到每两个变量之间存在 4 个字节的差异(即: >&x-&y=4&y-&z=4)。

我的问题是,为什么每两个变量之间的差异等于 4?

I have a question rather than a problem (witch maybe arises a memory question).. I've written this simple program:

#include <stdio.h>
#include <stdlib.h>

int multi(int x, int y);


int main(){
    int x;
    int y;
    printf("Enter the first number x: \n");
    scanf("%d",&x);
    printf("Enter the second number y: \n");
    scanf("%d",&y);
    int z=multi(x,y);
    printf("The Result of the multiplication is : %d\n",z,"\n");
    printf("The Memory adresse of x is : %d\n",&x);
    printf("The Memory adresse of y is : %d\n",&y);
    printf("The Memory adresse of z is : %d\n",&z);
    getchar();
    return 0;
}

int multi(int x,int y){
    int c=x*y;
    printf("The Memory adresse of c is : %d\n",&c);
    return c;  
}

As you can see (if you develop in C), this program inputs 2 int variables, then multiplies them with the multi function:

after getting the result , it displays the location of each variable in the memory (c,x,y and z).

I've tested this simple example those are the results (in my case):

The Memory adresse of c is : 2293556  
The Result of the multiplication is : 12  
The Memory adresse of x is : 2293620  
The Memory adresse of y is : 2293616  
The Memory adresse of z is : 2293612  

as you can see , the three variables x,y,z that are declared in the main function have closed memory adresses (22936xx) , the variable c that's declared in the multi function has a different adress (22935xx).

looking at the x,y and z variables, we can see that there's a difference of 4 bytes between each two variables (i.e : &x-&y=4, &y-&z=4).

my question is , why does the difference between every two variable equals 4?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

请别遗忘我 2024-10-13 20:45:29

xyz 是将在 调用堆栈(但见下文)。 sizeof int 是 4 个字节,这就是编译器将在堆栈上为这些变量分配多少空间。这些变量彼此相邻,因此它们相距 4 个字节。

您可以通过查找有关调用约定的信息来了解如何为局部变量分配内存。

在某些情况下(不使用取址运算符),编译器可能会将局部变量优化到寄存器中。

x, y, and z, are integer variables that will be created on the call stack (but see below). The sizeof int is 4 bytes, so that is how much space a compiler will allocate on the stack for those variables. These variables are adjacent to one another, so they are 4 bytes apart.

You can read about how memory is allocated for local variables by looking for information on calling conventions.

In some cases (where you do not use the address-of operator), the compiler may optimize local variables into registers.

强者自强 2024-10-13 20:45:29

在您的情况下,三个变量被分配在连续的内存块中。在 x86 系统上,int 类型为 32 位宽,即 sizeof(int) == 4。因此每个变量都与最后一个变量相距 4 个字节。

In your situation the three variables were allocated in contiguous memory blocks. On x86 systems, int types are 32-bits wide, i.e. sizeof(int) == 4. So each variable is placed 4 bytes apart from the last.

你的往事 2024-10-13 20:45:29

机器上机器字的大小为 4 字节,因此,为了程序的访问速度,它们在 4 字节边界上偏移每个变量。

The size of a machine word on your machine is 4 bytes so, for speed of access by your program they offset each variable on a 4 byte boundary.

甜是你 2024-10-13 20:45:29

局部变量分配在“堆栈”上。通常编译器会将它们按顺序排列,因为确实没有理由不这样做。 C 中的整数为 4 个字节。因此,yx 之后出现 4 个字节,而 zy 之后出现 4 个字节是有道理的。 。

Local variables are allocated on the "stack". Often the compiler will put them in sequential order since there's really no reason not to. An integer in C is 4 bytes. Therefore, it makes sense that y comes in 4 bytes after x, and z comes in 4 bytes after y.

森末i 2024-10-13 20:45:29

您似乎正在 32 位计算机上运行。每个int的大小为32位,一个字节为8位,一个int的大小为4个字节。每个内存地址对应一个字节,因此每个局部变量的地址相差4。

It appears that you are running on a 32-bit machine. The size of each int is 32 bits, and with 8 bits in a byte, the size of an int is 4 bytes. Each memory address corresponds to one byte, so there is a difference of 4 between the address of each local variable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文