问题描述
我对这三个概念有些困惑:堆栈,堆和动态内存分配.我将用C ++提供示例.
I have some confusion about these three concepts: Stack, Heap, and Dynamic memory allocation. I'll provide examples in C++.
-
我是说一个程序,对于它的所有变量,数组,也许还有堆栈上的对象,当程序刚刚启动时,所需的所有内存空间已经存在,所以所有的东西都是预先确定的吗?但是在程序运行时,对我来说,它仍然听起来像动态的",因为堆栈仍在变化,从某种意义上说,值仍在运行时被压入堆栈,从堆栈中弹出.
Am I correct to say that given a program, for all its variables, arrays, and maybe objects on stack, when the program just starting, all the memory space needed is already there, so everything is predetermined? But when the program is running, for me it still sounds like "dynamic" since a stack is still changing in a sense that values are still push into, pop off the stack, on the runtime.
关于堆,出于动态"的意义,我从这个站点上的一些答案中得到了这个想法,它是针对运行时确定的:
Regarding to heap, for the "dynamic" sense, I brought the idea from some answer on this site, it's for something determined on runtime:
cin >> box_size;
int *box = new int[box_size];
但是,那:
Box surprise_box = new Box();
我已经知道编译时需要多少空间了吗?但是它仍然在堆中.因此,似乎动态"内存分配正好成为程序员分配/分配内存的负担之一.
I already know how much space it's needed on compile time right? But it's still on the heap. So it seems like "Dynamic" memory allocation is just about the burden of allocation/deallocation of memory becomes one of programmer's.
&dagger ;:应该改为Box *ptr_surprise_box = new Box();
. (感谢您的评论)
†: It should be Box *ptr_surprise_box = new Box();
instead. (Thanks for the comment)
我理解为什么我的问题被认为过于笼统,但是我不知道如何将它们分成几部分.更糟糕的是,现在我正在考虑这是否与地址空间以及在编译时确定的内存地址有关.
I understand why my question is considered too broad, but I don't know how to split them into pieces. Even worse, now I'm considering whether this has something to do with address space and those memory address determined in compile time.
推荐答案
- 一般来说,是的.在程序开始时,调用一个函数时,大多数数据都在堆栈上,然后可以在堆上分配内存. (嗯,程序中不仅有
stack
或heap
,在其他段中也可能有全局变量,这是一个很长的故事.)
- Generally speaking, yes. At the beginning of your program, most of the data is on stack when you call a function, then you can allocate memory on heap. (Well, there is not only
stack
orheap
in a program, there may be also global variables in other segments, it is a long story).
堆栈空间是由编译器分配的,当您输入函数时,编译器生成的代码会自动为堆栈变量分配足够的空间.
The stack space is allocated by compiler, when you enter a function, the compiler generated code allocates enough space for the stack variables automatically.
堆空间由您分配,当您需要一些内存时,可以调用一个函数为您分配堆空间.
The heap space is allocated by you, when you need some memory, you call a function to allocate heap space for you.
- 指向内存地址的指针是堆栈上的变量
输入您的代码
cin >> box_size;
int *box = new int[box_size];
此处box
是堆栈上的变量. box
变量的值是指向堆上内存的指针.
Here box
is a variable on stack. The value of box
variable is a pointer to the memory on heap.
但是Box surprise_box = new Box();
是无效的语法.
示例:
-
Box box1;
:变量box1
在堆栈中.
Box *box2 = new Box();
:box2
的变量在堆栈上,它是一个指针,指向new Box()
Box *box2 = new Box();
: the variable of box2
is on stack, it is a pointer, points to the memory of new Box()
已更新:
没有关于堆"和堆"概念的明确的动态"定义.例如,在C99中,如果您执行scanf("%d", &size); int a[size]
;仍然可以,并且a
可以在堆栈上,而不是堆上.这完全取决于编译器的行为.您分配的内存通常在堆上(您称为内存分配函数,包括new()
),编译器代码分配的内存通常在堆栈上.
There is no clear "dynamic" definition with "heap" and "stack" concept. For example, in C99, if you do scanf("%d", &size); int a[size]
; it is still OK and a
can be on stack, not heap. It all depends on the compiler's behavior. Memory allocated by you is usually on heap (you called memory allocation functions, including new()
), memory allocated by the compiler's code is usually on stack.
ps:我认为@Slava的答案是相当不错的.这些概念在不同的领域. stack
和heap
主要与OS相关,dynamic
和static
主要与语言相关.我只是在谈论现代C ++语言的大多数实现,它们恰巧将new()
内存放入堆中,等等.
ps: I think @Slava's answer is quite good. These conepts are in different fields. stack
and heap
are mainly OS-related, dynamic
and static
are mainly language-related. I just talked about the most implementations of the modern C++ language, they happened to put new()
memory into heap, etc.
这篇关于堆栈,堆和动态内存分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!