Initialization of All Elements of an Array to One Default Value in C++

Initialization of all elements of an array to one default value in C++?

Using the syntax that you used,

int array[100] = {-1};

says "set the first element to -1 and the rest to 0" since all omitted elements are set to 0.

In C++, to set them all to -1, you can use something like std::fill_n (from <algorithm>):

std::fill_n(array, 100, -1);

In portable C, you have to roll your own loop. There are compiler-extensions or you can depend on implementation-defined behavior as a shortcut if that's acceptable.

How to initialize all members of an array to the same value?

Unless that value is 0 (in which case you can omit some part of the initializer
and the corresponding elements will be initialized to 0), there's no easy way.

Don't overlook the obvious solution, though:

int myArray[10] = { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 };

Elements with missing values will be initialized to 0:

int myArray[10] = { 1, 2 }; // initialize to 1,2,0,0,0...

So this will initialize all elements to 0:

int myArray[10] = { 0 }; // all elements 0

In C++, an empty initialization list will also initialize every element to 0.
This is not allowed with C until C23:

int myArray[10] = {}; // all elements 0 in C++ and C23

Remember that objects with static storage duration will initialize to 0 if no
initializer is specified:

static int myArray[10]; // all elements 0

And that "0" doesn't necessarily mean "all-bits-zero", so using the above is
better and more portable than memset(). (Floating point values will be
initialized to +0, pointers to null value, etc.)

C++ initialize array class with default value

This uses C++14 for std::make_index_sequence and the std::index_sequence it produces but you can make your own implementation as shown here. Using a delegating constructor you can add another constructor that takes an index_sequence so you can then expand the sequence and get a variadic list of values like

template<typename KEY, typename VAL, typename ALLOC=std::allocator<struct _internal>, size_t TBL_SIZE=100>
class open_hash_table{
private:
std::list<struct _internal, ALLOC> _table[TBL_SIZE];
template <std::size_t... Is>
open_hash_table(ALLOC allocator, std::index_sequence<Is...>)
: _table{ std::list<struct _internal, ALLOC>{((void)Is, allocator)}... } {}
public:
open_hash_table(ALLOC allocator=ALLOC())
: open_hash_table(allocator, std::make_index_sequence<TBL_SIZE>{}) {}
};

Your public constructor will call the private helper constructor and pass along an index_sequence that will have TBL_SIZE number of elements in it. Then in the delegating constructor the ((void)Is, allocator) part uses the comma operator to use each element of the index_sequence but we discard that in instead let the expression resolve to allocator. The (void)Is part casts the result of Is to void to suppress that it is unused. We have to use std::list<struct _internal, ALLOC>{ ... } as well because the constructor that takes an allocator is explicit so the type needs to be specified, no implicit conversion allowed.

Will C++ default-initialization set array elements to its default value?

Can I assume that C++ default-initialization set array elements to its default value?

No, for default initialization:

  • if T is an array type, every element of the array is default-initialized;

and the element type is int, then

  • otherwise, nothing is done: the objects with automatic storage duration (and their subobjects) are initialized to indeterminate
    values.

On the other hand, list initialization(since C++11) like int* p = new int[2]{}; or int* p = new int[2]{0};, or value initialization like int* p = new int[2](); will guarantee that, for int all the elements will be zero-initialized.

How to initialize only few elements of an array with some values?

Is it possible to skip this values and only assign the values 1, 2 and 3?

In C, Yes. Use designated initializer (added in C99 and not supported in C++).

int array[12] = {[0] = 1, [4] = 2, [8] = 3};  

Above initializer will initialize element 0, 4 and 8 of array array with values 1, 2 and 3 respectively. Rest elements will be initialized with 0. This will be equivalent to

 int array[12] = {1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0};   

The best part is that the order in which elements are listed doesn't matter. One can also write like

 int array[12] = {[8] = 3, [0] = 1, [4] = 2}; 

But note that the expression inside [ ] shall be an integer constant expression.

what is the default value of 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.

initial value of int array in C

If the array is declared in a function, then the value is undefined. int x[10]; in a function means: take the ownership of 10-int-size area of memory without doing any initialization. If the array is declared as a global one or as static in a function, then all elements are initialized to zero if they aren't initialized already.



Related Topics



Leave a reply



Submit