C++ 中静态数组和动态数组有什么区别?
我必须为我的班级做作业,它说不要使用静态数组,只能使用动态数组。书上和网上都看过了,还是不太明白。
我认为静态是在编译时创建的,动态是在运行时创建的,但我可能会误以为这是内存分配。
你能解释一下C++中静态数组和动态数组的区别吗?
I have to do an assignment for my class and it says not to use Static arrays, only Dynamic arrays. I've looked in the book and online, but I don't seem to understand.
I thought Static was created at compile time and Dynamic at runtime, but I might be mistaking this with memory allocation.
Can you explain the difference between static array and dynamic array in C++?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
静态数组在堆栈上创建,并具有自动存储期限:您不需要手动管理内存,但当它们所在的函数结束时它们会被销毁。它们在编译时必须具有固定大小:
使用
operator new[]
创建的数组具有动态存储持续时间,并存储在堆上(技术上“免费商店”)。它们在运行时可以有任何大小,但您需要自己分配和释放它们,因为它们不是堆栈帧的一部分:Static arrays are created on the stack, and have automatic storage duration: you don't need to manually manage memory, but they get destroyed when the function they're in ends. They necessarily have a fixed size at compile time:
Arrays created with
operator new[]
have dynamic storage duration and are stored on the heap (technically the "free store"). They can have any size during runtime, but you need to allocate and free them yourself since they're not part of the stack frame:static 是 C 和 C++ 中的关键字,因此 static 在应用于变量或数组时具有非常特定的含义,而不是一般的描述性术语。更令人困惑的是,它在不同的上下文中具有三种不同的含义。因此,静态数组可以是固定的,也可以是动态的。
让我解释一下:
第一个是 C++ 特有的:
两个是从 C 继承的:
在函数中,静态变量是在函数调用之间保留内存位置的变量。它是静态的,因为它仅初始化一次并在函数调用之间保留其值(使用静态使函数不可重入,即不是线程安全的)
在函数外部声明的静态变量是只能从以下位置访问的全局变量:在同一模块内(具有任何其他 #include 的源代码文件)
问题(我认为)您的意思是问动态数组和固定数组或编译时数组之间有什么区别。这是一个更简单的问题,编译时数组是提前确定的(在编译程序时)并且是函数堆栈框架的一部分。它们在主函数运行之前分配。动态数组在运行时使用“new”关键字(或 C 中的 malloc 系列)进行分配,并且它们的大小事先未知。在程序停止运行之前,动态分配不会自动清理。
static is a keyword in C and C++, so rather than a general descriptive term, static has very specific meaning when applied to a variable or array. To compound the confusion, it has three distinct meanings within separate contexts. Because of this, a static array may be either fixed or dynamic.
Let me explain:
The first is C++ specific:
Two are inherited from C:
within a function, a static variable is one whose memory location is preserved between function calls. It is static in that it is initialized only once and retains its value between function calls (use of statics makes a function non-reentrant, i.e. not threadsafe)
static variables declared outside of functions are global variables that can only be accessed from within the same module (source code file with any other #include's)
The question (I think) you meant to ask is what the difference between dynamic arrays and fixed or compile-time arrays. That is an easier question, compile-time arrays are determined in advance (when the program is compiled) and are part of a functions stack frame. They are allocated before the main function runs. dynamic arrays are allocated at runtime with the "new" keyword (or the malloc family from C) and their size is not known in advance. dynamic allocations are not automatically cleaned up until the program stops running.
对术语的含义有清晰的定义非常重要。不幸的是,静态和动态数组的含义似乎有多种定义。
静态变量是使用静态内存分配。这是一个独立于 C/C++ 的通用概念。在 C/C++ 中,我们可以创建具有全局、文件或局部作用域的静态变量,如下所示:
自动变量 通常使用基于堆栈的内存分配来实现。可以在 C/C++ 中创建自动数组,如下所示:
这些数组、
x、y、z
和w
的共同点是它们每个的大小是固定的并在编译时定义。了解自动数组和静态数组之间的区别很重要的原因之一是静态存储通常在 数据部分(或 BSS 部分)文件和编译器可以使用绝对地址来访问数组,这对于基于堆栈的存储来说是不可能的。
动态数组通常意味着不是可调整大小的数组,而是使用 动态内存分配,具有运行时确定的固定大小。在 C++ 中,这是使用
new
运算符< /a>.但是可以使用
alloca
创建一个在运行时定义固定大小的自动数组:对于真正的动态数组,应该在 C++ 中使用类似
std::vector
的东西(或者C 语言的可变长度数组)。OP问题中的作业意味着什么?我认为很明显,我们想要的不是一个静态或自动数组,而是一个使用
new
运算符使用动态内存分配的数组,或者使用例如std::vector 的非固定大小的数组
。It's important to have clear definitions of what terms mean. Unfortunately there appears to be multiple definitions of what static and dynamic arrays mean.
Static variables are variables defined using static memory allocation. This is a general concept independent of C/C++. In C/C++ we can create static variables with global, file, or local scope like this:
Automatic variables are usually implemented using stack-based memory allocation. An automatic array can be created in C/C++ like this:
What these arrays ,
x, y, z
, andw
have in common is that the size for each of them is fixed and is defined at compile time.One of the reasons that it's important to understand the distinction between an automatic array and a static array is that static storage is usually implemented in the data section (or BSS section) of an object file and the compiler can use absolute addresses to access the arrays which is impossible with stack-based storage.
What's usually meant by a dynamic array is not one that is resizeable but one implemented using dynamic memory allocation with a fixed size determined at run-time. In C++ this is done using the
new
operator.But it's possible to create an automatic array with a fixes size defined at runtime using
alloca
:For a true dynamic array one should use something like
std::vector
in C++ (or a variable length array in C).What was meant for the assignment in the OP's question? I think it's clear that what was wanted was not a static or automatic array but one that either used dynamic memory allocation using the
new
operator or a non-fixed sized array using e.g.std::vector
.我认为您的班级中使用的语义令人困惑。 “静态”的含义可能只是“恒定大小”,而“动态”的含义可能是“可变大小”。在这种情况下,恒定大小的数组可能如下所示:
“动态”数组只是允许底层存储在运行时增加或减少的任何一种结构。大多数时候,C++ 标准库中的
std::vector
类就足够了。像这样使用它:std::vector
定义了operator[]
,因此您可以使用与数组相同的语义来使用它。I think the semantics being used in your class are confusing. What's probably meant by 'static' is simply "constant size", and what's probably meant by "dynamic" is "variable size". In that case then, a constant size array might look like this:
and a "dynamic" one would just be any kind of structure that allows for the underlying storage to be increased or decreased at runtime. Most of the time, the
std::vector
class from the C++ standard library will suffice. Use it like this:std::vector
hasoperator[]
defined, so you can use it with the same semantics as an array.静态数组在编译时分配内存,内存在堆栈上分配。而动态数组是在运行时分配内存的,并且内存是从堆中分配的。
Static arrays are allocated memory at compile time and the memory is allocated on the stack. Whereas, the dynamic arrays are allocated memory at the runtime and the memory is allocated from heap.
我认为在这种情况下,这意味着它是静态的,因为大小是固定的。
使用 std::vector。它有一个 resize() 函数。
I think in this context it means it is static in the sense that the size is fixed.
Use std::vector. It has a resize() function.
您可以有一个伪动态数组,其中大小由用户在运行时设置,但之后是固定的。
You could have a pseudo dynamic array where the size is set by the user at runtime, but then is fixed after that.
静态数组:
动态数组:
Static Array :
Dynamic Array :
是的,静态数组是在编译时创建的,而动态数组是在运行时创建的。就其内存位置而言,静态内存位于堆栈上,动态内存位于堆上。位于堆上的所有内容都需要内存管理,除非存在垃圾收集器(如 .net 框架的情况),否则存在内存泄漏的风险。
Yes right the static array is created at the compile time where as the dynamic array is created on the run time. Where as the difference as far is concerned with their memory locations the static are located on the stack and the dynamic are created on the heap. Everything which gets located on heap needs the memory management until and unless garbage collector as in the case of .net framework is present otherwise there is a risk of memory leak.
静态数组:效率。不需要动态分配或释放。
C、C++ 中包含 static 修饰符的函数中声明的数组是静态的。
示例:静态 int foo[5];
Static array :Efficiency. No dynamic allocation or deallocation is required.
Arrays declared in C, C++ in function including static modifier are static.
Example: static int foo[5];
静态数组:
内存分配是在编译时完成的,内存分配在栈内存中
数组的大小是固定的。
动态数组:
内存分配是在运行时完成的,内存分配在堆内存中
数组的大小不固定。
static array:
The memory allocation is done at the complile time and the memory is allocated in the stack memory
The size of the array is fixed.
dynamic array:
The memory allocation is done at the runtime and the memory is allocated in the heap memory
The size of the array is not fixed.
让我们用一个函数来说明这个问题,
如果我们有静态数组,那么调用 function() 将从内存中迭代所有固定分配的组件。不会有任何追加。
另一方面,如果我们追加动态数组,它会扩展内存。
Let's state this issue with a function
if we have the static array then calling the function() will iterate all the fixed allocated components from the memory. There will be no append.
On the other hand, a dynamic array will extend the memory if we append it.
静态数组意味着在数组中给出元素,
动态数组意味着在数组中不给出元素
示例:
static arrary meens with giving on elements in side the array
dynamic arrary meens without giving on elements in side the array
example: