Reputation: 287
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
?
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? 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
EDIT: Problem solved, thank you all for the speedy answers!
Upvotes: 2
Views: 147
Reputation: 38228
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".
Upvotes: 9
Reputation: 726869
Is this valid for creating array1 size 1000 and array2 size 5000?
Yes, it is valid, because you are using 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++.
Will all array positions be set to NULL or 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[]
.
Upvotes: 3
Reputation: 44268
Yes operator new[]
is doing dynamic memory allocation and it is valid to pass a value whatever way you get it. No value will not be initialized and you should not forget to call delete[]
for that pointer or use a smart pointer.
Better way would be to use 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:
std::vector<int> array1( n[0], 123 );
std::vector<int> array2( n[1], 456 );
Upvotes: 2
Reputation: 213060
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:
std::vector<int> array1(n[0]);
std::vector<int> array2(n[1]);
Upvotes: 3
Reputation: 254641
Is this valid for creating
array1
size 1000 andarray2
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.
Will all array positions be set to NULL or 0?
No. new
will default-initialise the objects it creates; in the case of primitive types like int
, default-initialisation doesn't do anything, leaving them with indeterminate values.
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.
Upvotes: 1
Reputation: 122463
It's uninitalized. To initialize the elements to 0
, use
int *array1 = new int[n[0]]();
// ^^
Upvotes: 3