If I were to create an array with int* array = new int[10];
and fill part of the array with values, how can I check how much of the array is filled? I want to loop through and check if each value is the default value but I'm not sure what the default value of each array element is. Would it be null
0
or garbage values?
When an array is created without assigning it any elements, compiler assigns them the default value. Following are the examples: Boolean - false. int - 0.
int[] array = new int[5]; This array contains the elements from array[0] to array[4] . The elements of the array are initialized to the default value of the element type, 0 for integers.
The array data type is a compound data type represented by the number 8 in the database dictionary. Arrays store a list of elements of the same data type accessed by an index (element) number.
An array can also be initialized using a loop. The loop iterates from 0 to (size - 1) for accessing all indices of the array starting from 0. The following syntax uses a “for loop” to initialize the array elements. This is the most common way to initialize an array in C.
This is how to set a default value in C++ when making an array.
int array[100] = {0};
Now every element is set to 0
. Without doing this every element it garbage and will be undefined behavior if used.
Not all languages are like this. Java has default values when declaring a data structure but C++ does not.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With