C Static Array Initialization - How Verbose Do I Need to Be

C Static Array Initialization - how verbose do I need to be?

int foo[10] = {0};

This is very fine :)


Note that if you do the following:

int foo[10] = {1};

Only the first element of the array will be initialized with the non-zero number whereas the rest will be initialized with zeros.

C Static Array Initialization - how verbose do I need to be?

int foo[10] = {0};

This is very fine :)


Note that if you do the following:

int foo[10] = {1};

Only the first element of the array will be initialized with the non-zero number whereas the rest will be initialized with zeros.

Trouble initialising array in G++

int cate[catNum]= {1};

This syntax initializes the first element to 1 and the rest to 0. (Technically, it value-initializes the rest.)

Try,

std::fill( std::begin( cate ), std::end( cate ), 1 );

C char array initialization: what happens if there are less characters in the string literal than the array size?

This is not how you initialize an array, but for:

  1. The first declaration:

    char buf[10] = "";

    is equivalent to

    char buf[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  2. The second declaration:

    char buf[10] = " ";

    is equivalent to

    char buf[10] = {' ', 0, 0, 0, 0, 0, 0, 0, 0, 0};
  3. The third declaration:

    char buf[10] = "a";

    is equivalent to

    char buf[10] = {'a', 0, 0, 0, 0, 0, 0, 0, 0, 0};

As you can see, no random content: if there are fewer initializers, the remaining of the array is initialized with 0. This the case even if the array is declared inside a function.

Is each static array member initialized?

Yes.

szArray is an array of 4 elements, each of which is a char* pointer. Each of those 4 elements is in initalized to NULL.

What "recursively" means here is that, since data types can be arbitrarily complex (arrays within structures within unions within arrays, etc.), each member of an aggregate (array or structure) is initialized following the same rules.

  • szArray is an aggregate, so "every member is initialized (recursively) according to these rules".
    • szArray[0] through szArray[3] all have pointer type, so each of them "is initialized to a null pointer".

This (probably) does not involve any run-time recursion. On most systems, integer 0, floating-point 0.0, and null pointers are all represented as all-bits-zero, so a static aggregate object can probably be correctly initialized just by setting it to all-bits-zero. It's the definition that's recursive; the initialization of an aggregate object is defined in terms of the initializations of its elements/members, and so on recursively until you get down to individual scalars.

Initializing a std::array with a constant value

With std::index_sequence, you might do:

namespace detail
{
template <typename T, std::size_t ... Is>
constexpr std::array<T, sizeof...(Is)>
create_array(T value, std::index_sequence<Is...>)
{
// cast Is to void to remove the warning: unused value
return {{(static_cast<void>(Is), value)...}};
}
}

template <std::size_t N, typename T>
constexpr std::array<T, N> create_array(const T& value)
{
return detail::create_array(value, std::make_index_sequence<N>());
}

With usage

auto a = create_array<10 /*, int*/>(7); // auto is std::array<int, 10>

Which, contrary to std::fill solution, handle non default constructible types.

Initializing an array of doubles in C

Yes. arr[i] = 0; is correct. 0 will be implicitly converted to double type.

Explicit initialization of struct/class members

The empty braces form of initialization is standard in C++ (it's permitted explicitly by the grammar). See C Static Array Initialization - how verbose do I need to be? for more details if you're interested.

I assume that it was added to C++ because it might not be appropriate for a 0 value to be used for a default init value in all situations.

What is the fastest way to initialize an array in C with only two bytes for all elements?

IMO the easiest one to optimize by the compiler (and very safe as well) is

void foo(uint16_t color, uint8_t *arr16, size_t n)
{
uint8_t data_8_bit[2] = {color >> 8, color & 0xff};

while(n--)
{
memcpy(arr16 + n * 2, data_8_bit, 2);
}
}

https://godbolt.org/z/8Wh5Pc3aP

In C++, What should I do when I want to initialize class members via constructor when I initialize a class object array?

In C++11 you can use brace initializer:

cl* BB = new cl[3] {1, 2, 3};

or more verbose, so it's clear that the numbers are passed as arguments to constructors of different objects:

cl* BB = new cl[3] {{1}, {2}, {3}}; 

Though as you are allocating memory dynamically anyway, it's better to use std::vector, also it's more convenient if you want to initialize large number of objects with the same parameters:

std::vector<cl> BB(300, 3);

However, std::vector initialization won't compile if cl doesn't have a copy constructor. In that case you can use emplace_back():

vector<cl> BB;
BB.reserve(300);
for(int i = 0; i < 300; ++i)
BB.emplace_back(3);

This in turn can be wrapped into a template back_emplacer_iterator to use with std::fill_n



Related Topics



Leave a reply



Submit