3 ms·
char key[5] = { 0 }; Simple enough – this is supposed to zero the entire array, but instead it only zeroed the first four bytes. In C++ and on the stack "
by vmorgulis 11y ago
char key[5] = { 0 };
Simple enough – this is supposed to zero the entire array, but instead it only zeroed the first four bytes.
In C++ and on the stack "{0}" is not required as memory is zeroized. On heap (in a new) "{0,0,0,0,0}" should be used (or memset()). Not sure about that in C.
My understanding is that it's not a bug.
- mrich 11y ago> In C++ and on the stack "{0}" is not required as memory is zeroized. No, it isn't. Globals are. And since the explicit initializer is provided it definitely is a bug to not zero all bytes.
- adrianN 11y agoDo you only ever run debug builds? Memory not being initialised is one of the first gotchas you learn about in C/C++.
- MaulingMonkey 11y ago#include <iostream> int main() { int nope_not_initialized; std::cout << nope_not_initialized << "\n"; } cc1plus: warnings being treated as errors In function 'int main()': Line 5: warning: 'nope_not_initialized' is used uninitialized in this function ( http://codepad.org/6xrFQZuL http://codepad.org/6xrFQZuL ) Edit: And a C version. Amusingly, it will print "0" if I only use one printf statement: #include <stdio.h> int main() { int nope_not_initialized; printf("%d", nope_not_initialized); printf("%d", nope_not_initialized); } -142791388-142791388 Exited: ExitFailure 10 ( http://codepad.org/TgGfL7QW http://codepad.org/TgGfL7QW ) Edit x2: Apparently "implicit return 0 from main" is only a thing in C++? And here I'd assumed it was some silly carryover from C... ( http://codepad.org/JIH9ZteL http://codepad.org/JIH9ZteL )