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

32 questions
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
  • 885
  • 11
  • 33
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 nested struct initialization - what is initialized?

Let's say I have struct S: typedef struct { int x; int y; } S; Then: S s; // s is uninitialized here. And: S s = {}; // s is initialized (zeroed) here. Now let's say I have struct T: typedef struct { S s1; S s2; } T; And I initialize it…
Harry
  • 4,524
  • 4
  • 42
  • 81
1
vote
1 answer

Are struct scalar members zero-initialized when using value-initialization on a struct with a default non-trivial-constructor

If I have the following struct struct test { char *x; std::string y; }; And I initialize with test *t = new test(); That should value-initialize the object and do the following based on the standard: if T is a (possibly cv-qualified)…
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] = {}; };
Anne Quinn
  • 12,609
  • 8
  • 54
  • 101
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
  • 37,899
  • 23
  • 129
  • 288
0
votes
1 answer

c++11 - zero-initi of members insted of default-init

In the below code, I expect members of a being inited with gargabe as they are not mentioned in the members-init-list of the called constructor (with two int parameters). Instead, I'm constantly getting 0 in both i and j of a, b and c and I am…
0
votes
3 answers

the difference of automatic and dynamic variables rules in zero initialization

code like this, #include class obj { public: int v; }; int main(int argc, char *argv[]) { obj o1; std::cout << o1.v << std::endl; // print 32766, indeterminate values obj *o2 = new obj(); std::cout << o2->v <<…
0
votes
1 answer

how to initialize muti-dimension C++ std arrays including zero initialization?

I have a two dimensional std array such as, array, 9> tbl; How to initialize it with the same value such as -1? If I just want it to be zero-initialized, what is the best way to it?
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
  • 460
  • 3
  • 11
0
votes
1 answer

why does the first version print newline as 129 but the second works just fine?

when I run the first program, nl (newline) is set to 7ff, and prints out 129. #include // countblanks-tabs-newlinesv1.c void main() { long int c; unsigned char nl, space, tab = 0 ; while( ( c = getchar() ) != EOF) { …
simn7
  • 1
0
votes
1 answer

Static Initialization Order for Singletons

So I'm reading that for Zero Initialization will initialize: Every named variable with static or thread-local storage duration that is not subject to constant initialization, before any other initialization I am using a Singleton with the…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
0
votes
0 answers

Can you initialize a dynamically-sized variable-length array to zero?

In the book 21st Century C Tips From the New School. On page 171 it says, Now for the sad part: let us say that you have a variable-length array (i.e., one whose length is set by a runtime variable). The only way to zero it out is via memset : int…
Evan Carroll
  • 78,363
  • 46
  • 261
  • 468
-1
votes
2 answers

Initializing an Array in C++ : the fastest way?

What is the fastest way to initialize with zeros an array of int in C++? int plus_number[CA - 1]; for (int & i : plus_number) { i = 0; } or int plus_number[CA - 1] = {0}; or maybe there is another way?