【问题标题】:Revisited: difference between static array and dynamic array in C++?重温:C++中静态数组和动态数组的区别?
【发布时间】:2021-02-08 07:07:41
【问题描述】:

我是 C++ 的初学者,我看到了 here 的帖子。但是,我不清楚动态数组有什么好处。

一个好处是可以改变动态数组的长度,这里是代码

int *p = new int[10];
// when run out of the memory, we can resize
int *temp = new int[20];
copy(p, temp); // copy every element from p to temp
delete[] p; // delete the old array
p = temp;
temp = nullptr;

以上是动态分配,表示数组会在堆上,需要手动删除。但是,为什么不使用静态数组如下

int array1[10];
int *p = array1;
// when run out of the memory, we can resize
int array2[20];
copy(array1, array2); // copy every elements from array1 to array2;
p = array2;

在这段代码中,我们不需要删除array1,因为它在堆栈区。这是我的问题:

动态数组有什么好处?在我看来,调整大小不是一个大问题。人们总是说静态数组的大小是固定的,动态数组的大小是不固定的。为什么动态数组的大小不固定。比如int p=new int[10]p的大小是固定的。

非常感谢。

【问题讨论】:

  • 你知道变量的作用域,变量的生命周期吗?
  • @S.M.不是真的,这意味着如果静态数组在范围内,它将浪费内存。而动态数组在程序中删除会更加灵活?
  • 您提到的帖子是指堆栈和堆。所以尝试声明非全局int array[SOME_LARGE_NUMBER],如果你没有修改堆栈大小,这可能会失败。
  • 谢谢,我新建一个数组怎么样,先int array[LEN1],然后替换成int array[LEN2]

标签: c++ arrays dynamic static


【解决方案1】:
int array1[10];
int *p = array1;
// when run out of the memory, we can resize
int array2[20];
copy(array1, array2); // copy every elements from array1 to array2;
p = array2;

无论在哪个函数或内部范围内,array1array2 都被声明,当函数或内部范围返回时,这些数组会自动销毁。句号。

这就是为什么这被称为“自动范围”。可能存在指向其中一个数组的指针这一事实无关紧要。该数组将消失,任何取消引用该指针的尝试都将导致demons flying out of your nose

因此,如果您有任何宏伟的设计可以在从声明它们的函数返回后以某种形式或方式继续使用此数组,那就太糟糕了。这不会发生。

另一方面,在newing 之后,只要您正确跟踪指向newed 对象的指针,它们就可以在其他任何地方使用,直到它们得到deleted。这个功能,另一个功能,任何地方。甚至是不同的执行线程。

说了这么多,你也不应该使用newdelete。您应该使用 C++ 库的容器,它将为您正确处理所有内存分配、释放和复制。在这种情况下,您只是在重新发明 std::vector 已经为您所做的事情,而且它实际上会在某些方面做到这一点,比您自己轻松完成的效率要高得多。您只需致电resize(),然后,您的矢量会更大或更小,视情况而定。而且,在所有其他方面,向量将与您的数组无法区分。很难区分。

所以,使用 C++ 库的容器。他们是你的朋友。他们希望您代表您正确进行内存分配。现代 C++ 代码很少再使用newdelete。了解它的工作原理很重要,但 99% 的情况下您并不真正需要它。

【讨论】:

  • 我现在知道记忆的事了。感谢您的建议。
  • 您能否添加一些有关resizing 的详细信息。经常听说动态数组的长度可以调整大小。那是什么意思?在我看来int *p = new int[10]; 动态数组p 的大小也是固定的。我只是模拟了第一个代码块,并表明我们也可以将静态数组调整为动态数组(我的第二个代码块)。谢谢。
  • 我不知道你听到或没听到什么。 newed 数组的大小是固定的,这是正确的。如果需要改变它的大小,new 会创建一个新数组,所有内容都从旧数组复制到新数组,旧数组得到deleted。这是调整数组大小的唯一方法。这正是std::vector 将为您做的事情。你为什么要经历那么多麻烦,当std::vector已经为你辛苦了,你只需要打电话给resize()吗?
  • 感谢您的注意,我需要设计一个reisze() 只是为了做作业。
【解决方案2】:

new int[20]delete[] 等做你自己的动态数组,无疑有助于了解它是如何工作的。

在真正的 C++ 程序中,您将使用 std::vector。可能是这样的:

#include <iostream>
#include <string>
#include <vector>

int main() {
  std::vector<std::string> lines;

  std::string line;
  while (std::getline(std::cin, line)) {
    lines.push_back(line);
  }

  std::cout << "Read " << lines.size() << " lines of input\n";
}

您使用动态分配的原因是您的程序可以处理任意行数任意行长。这个程序可以读取四行或 400,000 行。 std::vector 是动态的。 std::string也是如此。

【讨论】:

  • 感谢您的来信。似乎我的第二段代码也做了调整大小的事情。但它可能还有另一个问题,因为在程序结束之前我无法删除array1[10]。在我看来,动态数组意味着它可以有任何长度。动态数组中的dynamic 是否意味着我们可以随时创建或删除?不是说动态数组的长度是dynamic?
【解决方案3】:

我写了一段静态和动态数组的代码,希望对你有帮助。

#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;
}

输出

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-18
    • 2012-09-14
    • 1970-01-01
    • 2019-09-27
    • 2014-10-08
    • 2020-07-13
    • 2011-02-09
    • 2012-05-09
    相关资源
    最近更新 更多