回顾:C++ 中静态数组和动态数组的区别?

Revisited: difference between static array and dynamic array in C++?

我是 C++ 的初学者,我看到了 post 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的大小是固定的

非常感谢。

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 得到声明,这些数组在函数或内部作用域 returns 时自动销毁。句号。

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

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

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

综上所述,您也不应该使用 newdelete。您应该使用 C++ 库的容器,它将为您正确处理所有内存分配、释放和复制。在这种情况下,您只是在重新发明 std::vector 已经为您完成的工作,而且它实际上会以某种方式完成,比您自己轻松完成的效率要高得多。您只需调用 resize(),然后,您的向量会根据具体情况变大或变小。而且,在所有其他方面,向量与您的数组没有区别。很难区分。

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

使用 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.

也是

I have write a code on static and dynamics array, hope this will help.

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

Output