c - scanf()如何将数据写入其他函数的堆栈帧?

标签 c memory

我了解如何使用 scanf(),但我很难理解 scanf() 如何访问其他函数及其堆栈帧。我知道 scanf() 必须提供一个用于写入数据的地址,但我认为每个函数都有自己的私有(private)数据,位于其自己的堆栈帧中。 以简单的程序段为例

#include <stdio.h>
int main() {
 int x;
 x = 2;
 scanf("%d",&x);
 printf("%d",x);
}

在这个程序中,main有自己的堆栈帧,其中局部变量x被初始化为2。我的理解是scanf()也有自己的堆栈框架。我知道 scanf() 被赋予了 x 的地址,但是 scanf() 如何允许更改 main 中的数据code> 的堆栈帧?

最佳答案

In this program, main has its own stack frame where local variable x is initialized to 2.

正确。

My understanding is that scanf() also has its own stack frame.

正确。

Scanf() is given the address of x, but how is scanf() allowed to change data in main's stack frame?

由谁或什么允许?没有什么可以阻止它这样做。它有地址。这是在同一个过程中。它可以简单地写入该地址。

关于c - scanf()如何将数据写入其他函数的堆栈帧?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51620281/

相关文章:

c - C 中的 LTRIM 函数

python - 使用 distutils 构建 C 扩展时打印编译器和链接器命令

iphone - 在线程中解除分配

c - 确定缓冲区的总大小(以字节为单位)

python - 当我释放由 CFFI 生成的 DLL 分配的 char* 时,为什么我的应用程序会崩溃?

c++ - 这个地址如何 (0x00000400) = 1024

c++ - 如何使用 Windows x64 记录堆栈帧

c - 左移 << 浮点变量

c - C中双斜杠后的分号

c - 将内存释放到C中的二维数组