100

C++中的静态数组和动态数组有什么区别?

我必须为我的班级做一个作业,它说不要使用静态数组,只能使用动态数组。我在书上和网上都看过,但我似乎不明白。

我认为静态是在编译时创建的,而动态是在运行时创建的,但我可能将其误认为是内存分配。

你能解释一下C++中静态数组和动态数组的区别吗?

4

12 回答 12

115

本地数组是在堆栈上创建的,并且具有自动存储持续时间——您不需要手动管理内存,但是当它们所在的函数结束时它们会被销毁。它们必须具有固定的大小:

int foo[10];

使用创建的数组operator new[]具有动态存储持续时间并存储在堆上(技术上是“免费存储”)。它们可以有任何大小,但您需要自己分配和释放它们,因为它们不是堆栈帧的一部分:

int* foo = new int[10];
delete[] foo;
于 2010-04-20T02:08:21.963 回答
39

static 是 C 和 C++ 中的关键字,因此当应用于变量或数组时,static 不是一般的描述性术语,而是具有非常特定的含义。为了使混淆更加复杂,它在不同的上下文中具有三个不同的含义。因此,静态数组可以是固定的也可以是动态的。

让我解释:

第一个是 C++ 特定的:

  • 静态类成员是未使用构造函数实例化或使用析构函数删除的值。这意味着必须以其他方式初始化和维护成员。静态成员可能是初始化为 null 的指针,然后在第一次调用构造函数时分配。(是的,那将是静态和动态的)

两个继承自 C:

  • 在函数中,静态变量是在函数调用之间保留其内存位置的变量。它是静态的,因为它只初始化一次并在函数调用之间保留其值(使用静态使函数不可重入,即不是线程安全的)

  • 在函数外部声明的静态变量是全局变量,只能从同一模块内访问(源代码文件与任何其他#include's)

您要问的问题(我认为)是动态数组与固定或编译时数组之间的区别。这是一个更简单的问题,编译时数组是预先确定的(在编译程序时)并且是函数堆栈框架的一部分。它们是在主函数运行之前分配的。动态数组在运行时使用“new”关键字(或 C 中的 malloc 系列)分配,并且它们的大小事先不知道。在程序停止运行之前,不会自动清理动态分配。

于 2013-07-23T15:30:17.760 回答
11

我认为您的课程中使用的语义令人困惑。“静态”可能意味着简单的“恒定大小”,而“动态”可能意味着“可变大小”。在这种情况下,一个恒定大小的数组可能如下所示:

int x[10];

而“动态”结构将是允许在运行时增加或减少底层存储的任何类型的结构。大多数时候,std::vector来自 C++ 标准库的类就足够了。像这样使用它:

std::vector<int> x(10); // this starts with 10 elements, but the vector can be resized.

std::vectoroperator[]定义,因此您可以使用与数组相同的语义。

于 2010-04-20T02:11:30.280 回答
11

明确定义术语的含义很重要。不幸的是,静态和动态数组的含义似乎有多种定义。

静态变量是使用静态内存分配定义的变量。这是一个独立于 C/C++ 的一般概念。在 C/C++ 中,我们可以创建具有全局、文件或局部范围的静态变量,如下所示:

int x[10]; //static array with global scope
static int y[10]; //static array with file scope
foo() {
    static int z[10]; //static array with local scope

自动变量通常使用基于堆栈的内存分配来实现。可以像这样在 C/C++ 中创建一个自动数组:

foo() {
    int w[10]; //automatic array

这些数组 、x, y, zw的共同点是它们每个的大小都是固定的,并且在编译时定义。

了解自动数组和静态数组之间的区别很重要的原因之一是静态存储通常在目标文件的数据部分(或BSS 部分)中实现,编译器可以使用绝对地址来访问数组这对于基于堆栈的存储是不可能的。

动态数组通常指的不是可调整大小的数组,而是使用动态内存分配实现的数组,其大小在运行时确定。在 C++ 中,这是使用new操作符完成的。

foo() {
   int *d = new int[n]; //dynamically allocated array with size n     

但是可以使用以下命令创建一个在运行时定义的具有修复大小的自动数组alloca

foo() {
    int *s = (int*)alloca(n*sizeof(int))

对于真正的动态数组,应该使用std::vectorC++ 中的东西(或 C中的可变长度数组)。

OP 问题中的作业意味着什么?我认为很明显,需要的不是静态或自动数组,而是使用new运算符使用动态内存分配或使用例如std::vector.

于 2014-10-20T13:15:22.890 回答
9

静态数组在编译时分配内存,内存分配在堆栈上。而动态数组在运行时分配内存,而内存是从堆中分配的。

int arr[] = { 1, 3, 4 }; // static integer array.   
int* arr = new int[3]; // dynamic integer array.
于 2010-04-20T02:08:36.373 回答
3

我认为在这种情况下,这意味着它是静态的,因为它的大小是固定的。使用 std::vector。它有一个 resize() 函数。

于 2010-04-20T02:06:33.230 回答
2

您可以有一个伪动态数组,其中大小由用户在运行时设置,但之后固定。

int size;
cin >> size;
int dynamicArray[size];
于 2014-10-17T03:55:46.633 回答
1

静态数组

  1. 静态数组在编译时分配内存。
  2. 大小是固定的。
  3. 位于堆栈内存空间。
  4. 例如。:整数数组[10];//大小为10的数组

动态数组:

  1. 内存是在运行时分配的。
  2. 大小不固定。
  3. 位于堆内存空间。
  4. 例如。: int* 数组 = 新的 int[10];
于 2020-09-15T11:21:59.010 回答
0

是的,静态数组是在编译时创建的,而动态数组是在运行时创建的。至于它们的内存位置的区别,静态位于堆栈上,而动态则在堆上创建。位于堆上的所有内容都需要内存管理,除非存在 .net 框架中的垃圾收集器,否则存在内存泄漏的风险。

于 2013-02-27T07:43:15.077 回答
0

静态数组:效率。不需要动态分配或释放。

在 C、C++ 中声明的数组在函数中包括静态修饰符是静态的。示例:静态 int foo[5];

于 2013-12-17T13:44:27.283 回答
-1

我已经在静态和动态数组上编写了代码,希望这会有所帮助。我已经使用 c++ 代码解释了如何在运行时更改动态数组的大小,但是一旦声明静态数组,我们就无法更改它的大小。

#include<iostream>
using namespace std;

int main (){
    //creating the static array .. rember the syntax of it.
    int array[4]= {1,2,3,4}; // size is fixed and can not be changeable at run time.
    
    cout<<"Static Array."<<endl;
    cout<<"Printing using index."<<endl;
    for(int x=0;x<4;x++){
        cout<<"\t"<<array[x];
    }
    
    cout<<endl;
    cout<<"Printing using pointer."<<endl;
    int*ptr= array;
    for(int x=0;x<4;x++){
        cout<<"\t"<<*ptr++;
    }
    //delete [] array ;// error, because we can not free the size from stack
//  array[6]= {1,2,3,4,5,6}; //Error: We can not change the size of static array if it already defined.
// we can not change the size of the static aray at run time.
    
    cout<<endl;
    cout<<"\n\nDynamic Array."<<endl; 
   int n=4;
   //Creating a dynamic Array, remember the systex of it.
   int *array2 = new int [n]; // size is not fixed and changeable at run time.
   array2[0]= 1;
   array2[1]= 2;
   array2[2]= 3;
   array2[3]= 4;
    cout<<endl;
    cout<<"Printing using index."<<endl;
    for(int x=0;x<4;x++){
        cout<<"\t"<<array2[x];
    }
    
    cout<<endl;
        cout<<"Printing using pointer."<<endl;
    int*ptr2= array2;
    for(int x=0;x<4;x++){
        cout<<"\t"<<*ptr2++;
    }
    cout<<endl<<endl<<endl;
    delete array2; //Size is remove at runtime
     cout<<"Chnaging the size of dynamic array at runtime... :)";
    // Changing the size of the array to 10.. at runtime 
    array2 = new int [10]; // Array size is now change to 10 at runtime
   array2[0]= 1;
   array2[1]= 2;
   array2[2]= 3;
   array2[3]= 4;
   array2[4]= 5;
   array2[5]= 6;
   array2[6]= 7;
   array2[7]= 8;
   cout<<endl;
    cout<<"Printing using index."<<endl;
    for(int x=0;x<7;x++){
        cout<<"\t"<<array2[x];
    }
    // free the memory/ heap
    delete [] array2;
    return 0;
}

输出

在此处输入图像描述

于 2022-02-22T05:26:16.543 回答
-4

静态数组与给定数组中的元素有关

动态数组 meens 不提供数组中的元素

例子:

     char a[10]; //static array
       char a[];  //dynamic array
于 2013-02-27T06:46:11.153 回答