我有一个问题而不是问题(女巫可能会出现内存问题)..我编写了这个简单的程序:
#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 变量,然后将它们与多功能:
得到结果后,显示每个变量在内存中的位置(c
,x
,y
和z )。
我已经测试了这个简单的示例,这些是结果(在我的例子中):
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)。
查看 x
、y
和 z
变量,我们可以看到每两个变量之间存在 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?
发布评论
评论(5)
x
、y
和z
是将在 调用堆栈(但见下文)。sizeof int
是 4 个字节,这就是编译器将在堆栈上为这些变量分配多少空间。这些变量彼此相邻,因此它们相距 4 个字节。您可以通过查找有关调用约定的信息来了解如何为局部变量分配内存。
在某些情况下(不使用取址运算符),编译器可能会将局部变量优化到寄存器中。
x
,y
, andz
, are integer variables that will be created on the call stack (but see below). Thesizeof 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.
在您的情况下,三个变量被分配在连续的内存块中。在 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.机器上机器字的大小为 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.
局部变量分配在“堆栈”上。通常编译器会将它们按顺序排列,因为确实没有理由不这样做。 C 中的整数为 4 个字节。因此,
y
在x
之后出现 4 个字节,而z
在y
之后出现 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 afterx
, andz
comes in 4 bytes aftery
.您似乎正在 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 anint
is 4 bytes. Each memory address corresponds to one byte, so there is a difference of 4 between the address of each local variable.