两个整型变量驻留在一个内存地址? [英] Two integer variables residing at one memory address?

查看:199
本文介绍了两个整型变量驻留在一个内存地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学习C指针,并正尝试解决的三分球练习可在网上。虽然下面的问题不使用指针,我的理解是不正确的输出是由于缺乏指针的使用。这里是的问卷

I am learning pointers in C, and am trying to solve exercises on pointers available online. Although the below question doesn't make use of pointers, I understand that the incorrect output is due to lack of usage of pointers. Here is the question-

/* p2.c 
Find out (add code to print out) the address of the variable x in foo1, and the 
variable y in foo2. What do you notice? Can you explain this? 
*/

这是我的答案 -

And here is my answer-

#include <stdio.h>
void foo1(int xval) 
{ 
 int x; 
 x = xval; 
 /* print the address and value of x here */ 
 printf("\n Address of variable x is:%p\n", &x);
 printf("\n Value of variable x is: %d\n", x); 

} 
void foo2(int dummy) 
{ 
 int y;
 y=dummy; 
 /* print the address and value of y here */ 
 printf("\n Address of variable y is:%p\n", &y);
 printf("\n Value of variable y is: %d\n", y); 
} 

int main() 
{ 
 foo1(7); 
 foo2(11); 

 return 0; 
} 

编译程序时的输出是─

The output when compiling the program is-

 Address of variable x is:0x7fff558fcb98

 Value of variable x is: 7

 Address of variable y is:0x7fff558fcb98

 Value of variable y is: 11

据我了解,当程序运行时,和 foo1 用一个整数作为函数的参数被调用时,整数 7 拍摄并存储在整数变量 X 。功能 foo1 然后打印变量 X 的地址和值。同样的事情功能重复 foo2的

From what I understand, when the program runs, and foo1 with an integer as function argument is called, the integer 7 is taken and stored in the integer variable x. The function foo1 then prints the address and value of variable x. Same thing repeats with function foo2.

不过,我不明白它是如何与不同的价值观两个整型变量驻留在相同的内存地址 0x7fff558fcb98 ?是不是因为内存管理问题在C(的malloc 免费等等 - 我现在还没有!)?

But, I couldn't understand how is it that two integer variables with different values are residing at the same memory address 0x7fff558fcb98? Is it because of memory management issues in C (malloc, free etc-I'm not there yet!)?

任何帮助将是非常美联社preciated。谢谢。

Any help would be highly appreciated. Thank You.

推荐答案

可视化栈的状态,了解行为是非常有用。当你在主,让我们说栈是这样的:

It's useful to visualize the state of the stack to understand the behavior. When you are in main, let's say the stack looks like:

当你输入 foo1 foo1 添加到堆栈中的堆栈帧,它看起来像:

When you enter foo1, the stack frame for foo1 is added to the stack and it looks like:

当您从 foo1 返回,堆栈将恢复为:

When you return from foo1, the stack will revert to:

当你输入 foo2的 foo2的添加到堆栈中的堆栈帧,它看起来像:

When you enter foo2, the stack frame for foo2 is added to the stack and it looks like:

堆栈帧的状态查找 foo1 非常相似, foo2的。这并不奇怪,局部变量的地址 X 是相同的。

The state of the stack frame looks very similar for foo1 and foo2. It's not surprising that the addresses of the local variables x and y are identical.

假如参数个数,参数类型或局部变量的类型是不同的,你会为局部变量的地址注意到不同的值。

Had the number of arguments, the arguments types or the local variable types been different, you would have noticed different values for the addresses of the local variables.

这篇关于两个整型变量驻留在一个内存地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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