Questions tagged [zero-initialization]

Please use this tag for asking questions related to initializing value of object to ints zero value type. For more details related to zero initialization please see https://en.cppreference.com/w/cpp/language/zero_initialization

  • For primitives zero-initialization creates them with a value-initialization of 0
  • For objects all of the primitives they are made up of are zero-initialized followed by default intialization
  • For arrays zero-initialization occurs using the rules above, based on whether the array elements are primitives or objects

For more information see: http://en.cppreference.com/w/cpp/language/zero_initialization

21 questions
26
votes
1 answer

C++ Zero-Initialization

I'm having trouble understanding when and why exactly a member in my class is zero-initialized according to http://en.cppreference.com/w/cpp/language/zero_initialization. Consider the following test program: #include #include…
Maximilian Gerhardt
  • 4,872
  • 3
  • 20
  • 42
25
votes
2 answers

Does std::unordered_map operator[] do zero-initialization for non-exisiting key?

According to cppreference.com, std::map::operator[] for non-existing value does zero-initialization. However, the same site does not mention zero-initialization for std::unordered_map::operator[], except it does have an example which relies on…
hyde
  • 50,653
  • 19
  • 110
  • 158
7
votes
1 answer

Zero initialiser for biases using get_variable in tensorflow

A code I'm modifying is using tf.get_variable for weight variables, and tf.Variable for bias initialisation. After some searching, it seems that get_variable should always be favoured due to its portability in regards to sharing. So I tried to…
4
votes
0 answers

Is a C-struct zero-initialized empty braces in C++?

If with C++17 I do a struct tm mytm{}; int i{}; Will the variable mytm (and to be on the safe side, also i) be zero-initialized? I am sent in circles through these pages: value-initialized, zero-initialized, default-initialized…
towi
  • 20,210
  • 25
  • 94
  • 167
4
votes
3 answers

Why doesn't initializing a C++ struct to `= {0}` set all of its members to 0?

After doing a ton of testing and writing this answer (note: the downvote on it was BEFORE my total rewrite of it), I can't understand why = {0} doesn't set all members of the struct to zero! If you do this: struct data_t { int num1 = 100; …
Gabriel Staples
  • 11,777
  • 3
  • 74
  • 108
4
votes
1 answer

Get a default-initialized (NOT value/zero-initialized) POD as an rvalue

#include struct A { int x; }; void foo(A a) { std::cout << a.x << std::endl; } int main() { A a; foo(a); // -7159156; a was default-initialized foo(A()); // 0; a was value-initialized } Is it possible to pass an…
3
votes
2 answers

Safely initializing a std::array of bools

Given this array declaration and initialization: std::array invalid_params{}; Can I assume that all the elements in the array will always be initialized to false, or is it better to do it explicitly?
anastaciu
  • 20,013
  • 7
  • 23
  • 43
3
votes
4 answers

Zeroing %EAX Register on the 8086

The first instruction I issued without fail at the start of every program for a DEC PDP-8 Minicomputer was CLA CLL to clear the accumulator and link (overflow) bit. That simple instruction doesn't seem to exist in 8086 range of processors and I have…
Android
  • 79
  • 1
  • 6
2
votes
2 answers

Why does some Windows booloader code zero registers with `sub` instead of `xor`?

Given considerations such as detailed in https://stackoverflow.com/a/33668295, it seems xor reg, reg is the best way to zero a register. But when I examine real-world assembly code (such as Windows bootloader code, IIRC), I see both xor reg, reg and…
2
votes
1 answer

Overly eager C++ union zero-initialization with constexpr

Below is a downstripped example of a tagged union template "Storage", which can assume two types L and R enclosed in a union, plus a bool indicating which of them is stored. The instantiation uses two different sized types, the smaller one actually…
2
votes
1 answer

C++ zero init an array of templates with variable array length

Is there a way to zero init an array of any type with a variable size? So what I want is to apply something like this: int results[5] = {}; to a template like this: T results[i] = {}; When I try this, my compiler says this: 'results' declared as…
user11914177
  • 677
  • 3
  • 21
2
votes
0 answers

Why is glmmTMP is estimating approx half value for Zero inflated Conway maxwell poisson mixed model for Simulated data

I'm trying to estimate parameter for Zero-inflated Conway Maxwell Poisson Mixed Model. I'm not getting why GlmmTMP function is giving approx half value for the non zero effect part and giving nice estimates for the Zero part and dispersion…
1
vote
1 answer

C++11 class member initialization

Just switched to C++11 from C++03, and I was wondering, is the following defined to always zero initialize the array data for all elements? template class Test { public: uint32 data[COUNT] = {}; };
1
vote
1 answer

When I 0-Initialize a vector Does It Have the Same Effect as calloc?

So calloc calls on the OS to retrieve zeroed pages on the heap: https://stackoverflow.com/a/2688522/2642059 What about C++11's vector constructor that only takes a size_t and 0-initializes the values? Ask the OS for a zeroed page in the general…
Jonathan Mee
  • 35,107
  • 16
  • 95
  • 241
0
votes
1 answer

Reset directly the content of a struct

I have implemented a function that resets the content of the structure to which a pointer points: template void initialize(Struct* s) { *s = Struct{}; } I have performance issues when Struct becomes big (above 10K) because…
Victor
  • 410
  • 2
  • 10
1
2