Why is the difference between int a [5] = {0} and int a [5] = {1} (missing function)

When we initialize an array like this int a[5] = {0} , the compiler does all 5 elements of 0. This is a really nice, compact initialization and a useful function.

But I wonder why the compiler does not initialize int a[5]={1} same way? Why doesn't he do all 5 elements 1? Why does the standard not provide for this? Is this not surprising? Is it not lost?

In addition, if the number of elements in the initializer is less than the size of the array, then compilation can initialize the remaining elements with the last element in the initializer. Values int a[5]={1,2,3} equivalent to int a[5]={1,2,3,3,3} . Similarly, int a[10]={1,2,3,0} equivalent to int a[10]={1,2,3,0,0,0,0,0,0,0}; .

Wouldn't all this be an amazing feature if the Standard empowers it? Or are there good reasons for this missing feature?


And there is something called a designated initializer on C99 that is used as:

Designated initializers can be combined with regular initializers, in the following example:

int a[10] = {2, 4, [8]=9, 10}

In this example, [0] is initialized to 2, a 1 is initialized to 4, and [2] - [7] are initialized to 0, and [9] is initialized to 10.

Quite interesting. But even this function is not in C ++.

+4
source share
4 answers

I personally find it more “logical” (i.e. simple) having a fixed default initializer instead of another rule for repeating the latter only for arrays. This may seem “practical” (that is, useful), but it’s IMO more logically complex.

However, I think you are making a big mistake when you try to apply logic to a language like C ++.

C ++ is a complex language whose rules are the result of a long history of evolution, and its current form is the result of the work of many people and even formal committees (only the last part could explain something ).

A language like C ++ cannot be inferred by logic; it needs to be studied as history. Unless you are Hari Seldon , you really cannot derive history using logical reasoning.

There are places in C ++ where you will suffer a lot if you try to use logic instead of learning. Just to name a few ...

  • Why is static sending by default (i.e. incorrect )?
  • Why is there no keyword for a null pointer?
  • Why is the difference between the two unsigned unsigned?
  • Why is the amount between the signed and the unsigned unmarked?
  • If unsigned means " Z_ element {2 ^ n} ", then why are unsigned dimensions?
  • Why std::string s; s=3.141592654; std::string s; s=3.141592654; great for c ++?
  • Why in C ++ 0X i = i++ + 1; undefined behavior works and i = ++i + 1; ?
  • Why double x=3.14; int y(int(x)); double x=3.14; int y(int(x)); doesn't mean y will be 3?
0
source

Why doesn't this make all 5 elements 1?

Because you don’t understand what {} means. (In fact, in C ++, the best way to do this is {} , not {0} ). The syntax {0} does not mean that you want all elements in the aggregate set to be zero. Rather, it says that you want an aggregate with the first null element assigned to the specified variable (which can be either an array or a class type in C ++). Since an aggregate usually has more fields than one zero value, the remaining elements in the aggregate are constructed by default. The default value for the built-in or POD type is to set all fields to zero, so you effectively set the entire population to zero.

Specifically, consider the following. In accordance with the current standard, none of the following statements will be fulfilled:

 struct abc { char field1; int field2; char field3; }; int main() { abc example = {'a', static_cast<int>('b')}; //All three asserts pass assert(example.field1 == 'a'); assert(example.field2 == static_cast<int>('b')); assert(example.field3 == '\0'); int example2[3] = {static_cast<int>('a'), 42}; assert(example2[0] == static_cast<int>('a')); assert(example2[1] == 42); assert(example2[2] == 0); } 

What would you expect field3 be in the proposed standard change? Even if you define it as the last element in the aggregated initializer, as you showed above, this will lead to a violation of compatibility with existing code, which assumes that the rest of the elements are built by default.


EDIT: I just realized that your question is asked in terms of arrays - but the answer is the same with any structures or arrays, so it really doesn't matter.

EDIT2: To keep this up to standard, class / structure references have been replaced with an “aggregate” below, which covers cases of structures and arrays.

+10
source

Yes, they could do it, but they did not, and it is too late to change this behavior. The decisions behind C and C ++ were made with the idea of ​​performance and minimalism at almost every step, so I believe that, if nothing else, also plays a role.

This feature just does not hit me, how amazing it all is. This is a very simple piece of syntactic sugar, and I rarely find the need to initialize such an array worthless except 0.

+4
source

Typical runtime libraries provide a function that simplifies data initialization to 0. In general, this is stored in a specific section of the executable file organized by the compiler and linker. When the program starts, the run-time code uses something like memset() to clear all initialized data to 0. This means that zero bytes should not be stored inside the executable itself.

The converse is that if you initialize the data with something other than zero, then the bytes for this data must be stored in the executable file itself, since the automatic initializer is only initialized to zero.

Therefore, if you declared a large char array (say megabytes?) And initialized it, say, {0} , then there would be no bytes in the executable file for this array. On the other hand, if you want to initialize it using {1} according to your scheme, a megabyte of 1 bytes must be stored in the executable file itself. By changing one character in the initialization list, the size of the executable file increases by megabytes.

I believe that such a scheme violates the principle of least surprise.

+2
source

Source: https://habr.com/ru/post/1336656/


All Articles