This content originally appeared on DEV Community and was authored by Calin Baenen
This was tested in SoloLearn's C++ Playground, I'm making the assumption my compiler will do something similar to the one SL uses.
So, I'm playing around with std::array<T, s>
, because I plan on using it in my game.
When I do std::array<bool, 5> def;
and print out the items in each index, I get:
Default 0: 0
Default 1: 0
Default 2: 0
Default 3: 0
Default 4: 0
When I do std::array<std::string, 5> def;
:
Default 0:
Default 1:
Default 2:
Default 3:
Default 4:
however, when I use an integral type like int
, I get numbers that are totally random in some of the spots. - Only a few are actually initialized with 0
(like I expect).
Here's a sample:
Default 0: 1875674416
Default 1: 32568
Default 2: 0
Default 3: 0
Default 4: 4199168
So, is there a way I can get these integral types to initialize properly? Or do I have to manually initialize the array with 0
(somehow)?
This content originally appeared on DEV Community and was authored by Calin Baenen

Calin Baenen | Sciencx (2021-11-10T23:40:11+00:00) Why do integral types get weirdly initialized inside of std::array in C++, and how can I fix it?. Retrieved from https://www.scien.cx/2021/11/10/why-do-integral-types-get-weirdly-initialized-inside-of-stdarray-in-c-and-how-can-i-fix-it/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.