[英]Is this dynamic array allocation allowed
int n[] = {1000,5000,100000,105000,400000,500000,505000,800000,995000,1000000};
int *array1 = new int[n[0]]; //1000
int *array2 = new int[n[1]]; //5000
Is this valid for creating array1
size 1000
and array2
size 5000
? 这对于创建
array1
size 1000
和array2
size 5000
吗?
Even after programming for a year this stuff still trips me up. 即使经过一年的编程,这些东西仍然让我兴奋不已。
And my second question: Is there any way to automatically set all values in each position to NULL or 0? 我的第二个问题:有没有办法自动将每个位置的所有值设置为NULL或0? Because after printing the array, the first chunk of numbers are:
因为在打印数组后,第一个数字块是:
1163089152
1330794578
1162627398
1547322173
1919251285
1766218867
7367020
1129595223
1128090959
1635212346
1836016500
1852405504
1030908260
1465662019
1868852841
29559
1625020798
134224442
4199212
4234532
4234544
4209436
4200378
4286800
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1910233198
134224636
1985292544
1985292576
1985292608
1985292640
1985292672
1985292704
1985292736
1985292768
1985292800
1985292832
1985292864
1985292896
1985292928
1985292960
1985292992
1985293024
1985293056
1985293088
1985293120
1985293152
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
... .... ..... ...... .....
And in my current assignment efficiency is key. 在我目前的任务中,效率是关键。 Looping through 1000 elements just to set them to 0 or NULL seems...inefficient
循环遍历1000个元素只是为了将它们设置为0或NULL似乎......效率低下
EDIT: Problem solved, thank you all for the speedy answers! 编辑:问题解决了,谢谢大家的快速答案!
No, the values aren't initalized to any specific values. 不,这些值不会被赋予任何特定值。 You can value* initialize them if you desire:
如果您愿意, 您可以初始化它们 :
int *array1 = new int[n[0]](); // Introduced in C++98 int *array1 = new int[n[0]]{}; // Introduced in C++11
That will initialize all values to zero. 这会将所有值初始化为零。
*"default initialization" in this context is a colloquial misnomer for "value initialization" , which was introduced in C++03 and is almost always what is meant when one says "default initialization". *“默认初始化”在这个上下文中是“值初始化”的口语误称,它在C ++ 03中引入,并且几乎总是当人们说“默认初始化”时的含义。
Yes, it's fine, but if you want the array data to be initialised to zero then you need: 是的,没关系,但是如果你想将数组数据初始化为零,那么你需要:
int *array1 = new int[n[0]]();
int *array2 = new int[n[1]]();
^^^^
You might want to consider using slightly more up-to-date C++ idioms though - std::vector
would probably be a better choice than raw arrays: 你可能想考虑使用稍微更新的C ++习语 -
std::vector
可能是比原始数组更好的选择:
std::vector<int> array1(n[0]);
std::vector<int> array2(n[1]);
It's uninitalized. 这是没有经营的。 To initialize the elements to
0
, use 要将元素初始化为
0
,请使用
int *array1 = new int[n[0]](); // ^^
Is this valid for creating array1 size 1000 and array2 size 5000?
这对于创建array1 size 1000和array2 size 5000有效吗?
Yes, it is valid, because you are using operator new[]
. 是的,它是有效的,因为您正在使用operator
new[]
。 If you declared int array1[n[0]]
, you would be relying on an extension, because variable-length arrays are not supported in C++. 如果您声明了
int array1[n[0]]
,那么您将依赖于扩展,因为C ++中不支持可变长度数组。
Will all array positions be set to NULL or 0?
将所有数组位置设置为NULL还是0?
Neither. 都不是。 The values will be undefined (that's a fancy way of saying "garbage") until you assign to them.
在您分配这些值之前,这些值将是未定义的(这是一种说“垃圾”的奇特方式)。 You can force initialization by appending a pair of parentheses after the call of
new[]
. 您可以通过在调用
new[]
之后附加一对括号来强制初始化。
Yes operator new[]
is doing dynamic memory allocation and it is valid to pass a value whatever way you get it. 是
operator new[]
正在进行动态内存分配,无论以何种方式传递值都是有效的。 No value will not be initialized and you should not forget to call delete[]
for that pointer or use a smart pointer. 没有值不会被初始化,你不应该忘记为该指针调用
delete[]
或使用智能指针。
Better way would be to use std::vector
- 更好的方法是使用
std::vector
-
std::vector<int> array1( n[0] );
std::vector<int> array2( n[1] );
you do not need to worry on memory deallocation and data will be initialized. 您不必担心内存释放,数据将被初始化。 If you need to initialize some different value than 0, just pass it as a second parameter:
如果需要初始化一些不同于0的值,只需将其作为第二个参数传递:
std::vector<int> array1( n[0], 123 );
std::vector<int> array2( n[1], 456 );
Is this valid for creating
array1
size 1000 andarray2
size 5000?这对于创建
array1
size 1000和array2
size 5000有效吗?
Yes. 是。 But you'll be better off using automatically managed dynamic arrays:
但是你最好使用自动管理的动态数组:
std::vector<int> array1(1000);
to save yourself the bother of remembering to delete the array once you've finished with it. 一旦你完成它,就要记住删除数组的麻烦。 It can be surprisingly difficult to get that right without RAII .
如果没有RAII ,那就很难做到这一点。
Will all array positions be set to NULL or 0?
将所有数组位置设置为NULL还是0?
No. new
will default-initialise the objects it creates; new
将默认初始化它创建的对象; in the case of primitive types like int
, default-initialisation doesn't do anything, leaving them with indeterminate values. 在像
int
这样的基本类型的情况下,default-initialisation不会做任何事情,使它们具有不确定的值。
If you want them to be value-initialised to zero, then you can specify that: 如果您希望将它们的值初始化为零,那么您可以指定:
int *array1 = new int[n[0]]();
^^
or use vector
, which will value-initialise them unless you specify another initial value. 或者使用
vector
,除非您指定另一个初始值,否则它将对它们进行值初始化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.