gpt4 book ai didi

assembly - 我应该使用堆栈进行长期变量存储吗?

转载 作者:行者123 更新时间:2023-12-03 06:44:48 25 4
gpt4 key购买 nike

根据《汇编语言循序渐进》(第3版)中的“短期存储”第8章:

The stack should be considered a place to stash things for the short term. Items stored on the stack have no names, and in general must be taken off the stack in the reverse order in which they were put on. Last in, first out, remember. LIFO!

但是,据我所知,C 编译器基本上使用堆栈来处理所有事情。这是否意味着堆栈是存储短期和长期变量的最佳方式?或者有更好的办法吗?

我能想到的替代方案是:

  • 堆,但速度很慢。
  • 静态变量,但这将持续程序的整个生命周期,这可能会浪费大量内存。

最佳答案

堆栈通常用于将参数推送到函数调用,存储函数的局部变量,并且还跟踪返回地址(从当前函数返回后将开始执行的指令)。但是,函数调用的实现方式取决于编译器的实现和 calling conventions

C compilers use the stack for basically everything

事实并非如此。 C编译器不会将全局变量和静态变量放入堆栈中。

Does that mean that the stack is the best way of storing variables, both short term and long term?

堆栈应该用于当前函数返回后不再使用的变量。是的,您也可以长期使用堆栈。 main() 中的局部变量将在程序的整个生命周期中持续存在。另请记住,每个程序的堆栈都是有限的。

Heap, but that's slow.

那是因为它需要在运行时进行一些管理。如果您想在程序集中分配堆,您将必须自己管理堆。在 C、C++ 等高级语言中,语言运行时和操作系统管理堆。您不会在 assembly 中看到它。

关于assembly - 我应该使用堆栈进行长期变量存储吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41842938/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com