C/ c++动态或静态内存分配?

时间:2022-09-06 15:18:51

Dynamic memory allocation in C/C++ happens through malloc and the static memory allocation ex: int a[3]; its allocated after the code is executed.

C/ c++中的动态内存分配通过malloc和静态内存分配发生:int a[3];它在执行代码后分配。

But this code int x[y+1]; only can happen after a value is attributed to y and this happens in execution time, so its static, dynamic or both? does the compiler insert a malloc in the machine code automatically?

但是这个代码int x[y+1];只有在一个值被归为y之后才会发生,这在执行时发生,所以它是静态的,动态的还是同时发生的?编译器会自动在机器代码中插入一个malloc吗?

3 个解决方案

#1


4  

It is a Variable Length Array (VLA). Wikipedia: http://en.wikipedia.org/wiki/Variable-length_array

它是一个可变长度数组(VLA)。*:http://en.wikipedia.org/wiki/Variable-length_array

Technically, it is not legal in C++, but compilers often support it as an extension, but generate warnings when they are turned on. See Why aren't variable-length arrays part of the C++ standard?

从技术上讲,它在c++中是不合法的,但是编译器通常支持它作为扩展,但是当它们被打开时就会生成警告。明白为什么变量长度数组不是c++标准的一部分吗?

It is legal in C.

在C语言中是合法的。

#2


0  

int[] is on the stack, while malloc'd or new'd things are on the heap.

int[]在堆栈上,而malloc或新的东西在堆上。

VERY basically int[] gets allocated automatically when it is reached (where y is already known) and gets dropped when it gets out of scope. It is not everything already allocated at startup.

基本上,int[]在到达(y已经知道)时自动分配,当它超出范围时就会被删除。它并不是在启动时已经分配的所有内容。

There are no hidden malloc calls or stuff, that is just how the stack memory works.

没有隐藏的malloc调用或内容,这就是堆栈内存的工作方式。

(I hope for an answer from someone who actually knows C/C++)

(我希望能得到一个真正了解C/ c++的人的回答)

#3


-1  

int x[y+1] can't happen as you're thinking of it. You can only have int x[SOME_CONSTANT_HERE]. If y was declared const, then this would work.

当你想到它时,就不会发生。只能有int x[SOME_CONSTANT_HERE]。如果y被宣布为const,那么这将会起作用。

That's because there is no magic in C/C++ -- file-scoped items will only get allocated at compile-time, and local items will get pushed & popped on the stack (as Felk mentioned) as they go in and out of scope (i.e., as execution enters and leaves a block). But the latter is not dynamic allocation in the way you're thinking of it, via malloc(); it's the (user) stack.

这是因为在C/ c++中没有魔法——文件范围的项目只会在编译时分配,而本地项会被推到堆栈上(正如Felk提到的那样),因为它们会进入和超出范围(也就是)。,当执行进入和离开一个块时。但后者不是通过malloc()的方式进行动态分配的;(用户)的堆栈。

(Note: apparently some C99 compilers will let you declare an array with a variable size if the array is a local variable, and thus created on the user stack. It is definitely an error if you try to do this with a file-scoped variable, however.)

(注意:显然,如果数组是局部变量,并且在用户堆栈上创建,那么一些C99编译器将允许您声明一个具有可变大小的数组。但是,如果您尝试使用一个文件作用域的变量来执行这个操作,那绝对是一个错误。

#1


4  

It is a Variable Length Array (VLA). Wikipedia: http://en.wikipedia.org/wiki/Variable-length_array

它是一个可变长度数组(VLA)。*:http://en.wikipedia.org/wiki/Variable-length_array

Technically, it is not legal in C++, but compilers often support it as an extension, but generate warnings when they are turned on. See Why aren't variable-length arrays part of the C++ standard?

从技术上讲,它在c++中是不合法的,但是编译器通常支持它作为扩展,但是当它们被打开时就会生成警告。明白为什么变量长度数组不是c++标准的一部分吗?

It is legal in C.

在C语言中是合法的。

#2


0  

int[] is on the stack, while malloc'd or new'd things are on the heap.

int[]在堆栈上,而malloc或新的东西在堆上。

VERY basically int[] gets allocated automatically when it is reached (where y is already known) and gets dropped when it gets out of scope. It is not everything already allocated at startup.

基本上,int[]在到达(y已经知道)时自动分配,当它超出范围时就会被删除。它并不是在启动时已经分配的所有内容。

There are no hidden malloc calls or stuff, that is just how the stack memory works.

没有隐藏的malloc调用或内容,这就是堆栈内存的工作方式。

(I hope for an answer from someone who actually knows C/C++)

(我希望能得到一个真正了解C/ c++的人的回答)

#3


-1  

int x[y+1] can't happen as you're thinking of it. You can only have int x[SOME_CONSTANT_HERE]. If y was declared const, then this would work.

当你想到它时,就不会发生。只能有int x[SOME_CONSTANT_HERE]。如果y被宣布为const,那么这将会起作用。

That's because there is no magic in C/C++ -- file-scoped items will only get allocated at compile-time, and local items will get pushed & popped on the stack (as Felk mentioned) as they go in and out of scope (i.e., as execution enters and leaves a block). But the latter is not dynamic allocation in the way you're thinking of it, via malloc(); it's the (user) stack.

这是因为在C/ c++中没有魔法——文件范围的项目只会在编译时分配,而本地项会被推到堆栈上(正如Felk提到的那样),因为它们会进入和超出范围(也就是)。,当执行进入和离开一个块时。但后者不是通过malloc()的方式进行动态分配的;(用户)的堆栈。

(Note: apparently some C99 compilers will let you declare an array with a variable size if the array is a local variable, and thus created on the user stack. It is definitely an error if you try to do this with a file-scoped variable, however.)

(注意:显然,如果数组是局部变量,并且在用户堆栈上创建,那么一些C99编译器将允许您声明一个具有可变大小的数组。但是,如果您尝试使用一个文件作用域的变量来执行这个操作,那绝对是一个错误。