gpt4 book ai didi

c++ - 动态数组和普通数组的区别

转载 作者:行者123 更新时间:2023-11-28 00:44:22 25 4
gpt4 key购买 nike

<分区>

我是初学者。我对它们之间的区别感到困惑。我已经阅读了一些答案并意识到其中一个区别是可以删除动态数组而普通数组不能。但是还有其他区别吗?比如它们的功能、大小或其他什么?

好吧,我读过这样的例子,如果我用普通数组 {int p [i];} 替换动态数组 {p= new (nothrow) int[i];} ,我看不出有什么区别.

    #include <iostream>
#include <new>
using namespace std;
int main ()
{
int i,n;
int * p;
cout << "How many numbers would you like to type? ";
cin >> i;
p= new (nothrow) int[i];
if (p == 0)
cout << "Error: memory could not be allocated";
else
{
for (n=0; n<i; n++)
{
cout << "Enter number: ";
cin >> p[n];
}
cout << "You have entered: ";
for (n=0; n<i; n++)
cout << p[n] << ", ";
delete[] p;
}
return 0;
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com