函数参数的内存位置

发布于 2024-12-26 06:03:46 字数 280 浏览 2 评论 0 原文

我正在准备 UNIX 考试,有一道关于 C 变量的内存位置的问题。 假设我们有这样的代码

char sth;
int some_function(int arg) {
   int some_int;
   // some code here
}

,所以我假设 sth 位于堆上,some_int 位于堆栈上,但是 arg 位于哪里? 有人可以解释一下 C 变量是如何管理的吗?

谢谢

I'm preparing for my UNIX exam and there is a question about memory location of C variables.
Let's say we have code like this

char sth;
int some_function(int arg) {
   int some_int;
   // some code here
}

so I suppose that sth is located on the heap, some_int on the stack, but where is arg located?
Can somebody please explain how are C variables managed?

Thank you

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

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

发布评论

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

评论(7

榆西 2025-01-02 06:03:46

请注意,这一切都取决于实现。 C 标准甚至没有提到栈、堆等词。它只是讨论变量根据其存储(staticexternregister 等)所期望的行为。

话虽如此,通常 arg 将位于为函数提供的堆栈帧中。它的作用域仅限于函数,就像 some_int 的作用域一样。

顺便说一下,sth 不在堆上,它有一个静态全局存储。

Note that everything of this is implementation dependent. The C standard does not even utter the words stack, heap and so on. It just talks about the behavior that is expected from variables depending on their storage(static,extern,register etc).

Having said so usually arg will be located in the stack frame which is provided for the function. It's scope is limited to the function just as scope of some_int.

By the way sth is not on heap it has a static global storage.

妞丶爷亲个 2025-01-02 06:03:46

这里的一切都完全依赖于平台,实际上与 C 语言无关,而是与我的编译器如何执行它有关。

sth 具有静态(全局)存储,因此它可能不在堆上,而是在全局数据段中。 some_int 确实位于 some_function 的本地堆栈帧中。变量 arg 填充在 some_function 中,但它的位置取决于编译器以及通常所说的“调用约定”:它可以在以下位置分配和清理:调用者或被调用者的堆栈帧,以及调用者或被调用者的堆栈帧,具体取决于约定,或者在寄存器中传递,根本不进入内存。

Everything here is totally platform dependent and really not about C the language, but about How My Compiler Does It.

sth has static (global) storage, so its probably not on the heap, but rather in the global data segment. some_int is indeed in the local stack frame of some_function. The variable arg is populated within some_function, but where it lives is up to the compiler and what's usually known as the "calling convention": It may be allocated and cleaned up in the stack frame of the caller or the callee, and by the caller or the callee, depending on conventions, or passed in a register and not go into memory at all.

柒夜笙歌凉 2025-01-02 06:03:46

arg 将位于堆栈中(至少对于桌面平台而言)。

阅读一篇名为“smashing the stack for fun andprofit”的文档,您将了解 C 中的内存是如何管理的。

arg will be located in the stack (for desktop platforms at least).

Read a document called "smashing the stack for fun and profit" and you will understand how the memory is managed in C.

記柔刀 2025-01-02 06:03:46

sth 位于静态内存中,argsome_int 位于堆栈中。当调用 some_function 时,arg 被复制(“推送”)到堆栈。堆是动态内存,包含在运行时分配的数据(例如使用malloc)。

sth is in the static memory, arg and some_int are in the stack. arg is copied ("pushed") to the stack when some_function is called. The heap is dynamic memory and contains data allocated at the run time (using malloc for example).

最美不过初阳 2025-01-02 06:03:46

参数(除了极少数例外)在堆栈上传递。

您应该能够通过以下操作来验证它们是否在您的计算机体系结构中:

printf("%p - %p\n", &arg, &some_int);

它们通常应在彼此之间的几个字节之内。

编辑:正如其他人所指出的,某物不是在堆上分配的,而是在程序的数据段中分配的,即编译器已经在编译时分配了内存。

Arguments are (with very few exceptions) passed on the stack.

You should be able to verify that they are in your computer architecture by just doing;

printf("%p - %p\n", &arg, &some_int);

They should normally be within a few bytes of each other.

Edit: As others have noted, sth is not allocated on the heap, but in the program's data segment, ie the compiler has already allocated the memory at compile time.

云巢 2025-01-02 06:03:46

某物可能位于块静态存储(又名“BSS”,具体取决于平台)中:

再次,这完全是“平台相关的”,但通常有四个区域“段”,您可以在其中分配变量空间:

a) 堆:您的语言的运行时管理“堆”数据,例如调用“malloc()”或“new” ”)

b)堆栈:这些是“自动”变量

c) BSS:未初始化(变量)静态数据

d) 数据:已初始化(通常是只读)静态数据

http://en.wikipedia.org/wiki/Data_segment

sth is probably in block static storage (a.k.a. "BSS", depending on platform):

Again, this is entirely "platform dependent", but there are generally four regions "segments" where you can allocate variable space from:

a) Heap: your language's runtime manages "heap" data, e.g. with calls to "malloc()" or "new")

b) Stack: these are "automatic" variables

c) BSS: unintialized (variable) static data

d) Data: initialized (and often read-only) static data

http://en.wikipedia.org/wiki/Data_segment

凝望流年 2025-01-02 06:03:46

这取决于实施;参数可以被推入堆栈帧,或者可以被写入寄存器,或者可以通过某种其他机制传递。

语言定义并没有规定各种对象应该存储在哪里;它仅规定这些对象的行为方式。

It depends on the implementation; arguments may be pushed onto the stack frame, or they may be written to registers, or they may be passed by some other mechanism.

The language definition does not mandate where various objects should be stored; it only mandates how those objects should behave.

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