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
92
votes
5 answers

Why does the first element outside of a defined array default to zero?

I'm studying for the final exam for my introduction to C++ class. Our professor gave us this problem for practice: Explain why the code produces the following output: 120 200 16 0 using namespace std; int main() { int x[] = {120, 200, 16}; …
33
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
  • 60,639
  • 21
  • 115
  • 176
31
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
  • 5,188
  • 3
  • 28
  • 61
19
votes
1 answer

Does C++ standard guarantee the initialization of padding bytes to zero for non-static aggregate objects?

Does C++ support a language construct that will allow us to initialize an object and all its padding fields to zero. I found some encouraging wording in cppreference.com about zero-initialization that suggests that on some conditions, the padding…
Shriram V
  • 1,500
  • 2
  • 11
  • 20
9
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 on how to initialize a struct to zero in C++ (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…
Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265
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

When using yarn-berry, should I apply the unplugged to the git repository when implementing zero-installs?

When using yarn-berry, should I apply the unplugged to the git repository when implementing zero-installs? As far as I know, in the case of zero-installs, we proceed with yarn start immediately after receiving git clone from the git…
loare
  • 59
  • 7
4
votes
2 answers

C automatically appending null character to a string?

#include #include int main(int argc, const char * argv[]) { int i; char s1[100] = "Computer Programming Class"; char s2[100] = "ECE"; int length = (int)strlen(s1); for (i = 0; i < length; i++) { …
4
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
  • 23,467
  • 7
  • 28
  • 53
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
  • 21,587
  • 28
  • 106
  • 187
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
1 answer

Using `-ftrivial-auto-var-init` to guarantee the initialization of padding bytes to zero for non-static aggregate objects

I'm following Does C++ standard guarantee the initialization of padding bytes to zero for non-static aggregate objects? and trying to force GCC and Clang to initialize padding with zero. The -ftrivial-auto-var-init=choice flag seems to do the trick,…
Leonardo
  • 1,533
  • 17
  • 28
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
1 answer

What is in the PTE address field for an anonymously zero-fill-on-demand mapped page?

When a program calls mmap to allocate an anonymous page, also known as a demand-zero page, what appears in the address field of the corresponding page table entry (PTE)? I am assuming that the kernel does not create a zero-initialized page in…
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…
1
2 3