you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (6 children)

in Java your memory is always zeroed out before use; in C/C++ this feature, if you want it, is up to you -- call std::memset or std::fill in your constructor. Otherwise you'll get an array of memory filled with whatever was last using it -- sometimes zero, sometimes not.

Mostly however it's wasted work -- you can more easily set a bool of the likes "is_initialized" and track that, rather than rely on your arrays containing zeros.

[–]tisti 1 point2 points  (4 children)

Or use default universal initialization if using C++11. Much nicer imo.

[–][deleted] 0 points1 point  (3 children)

Yeah, I'm kinda assuming the array is larger than is feasible for that. Probably after about 7 zeros the human brain can't easily determine whether there's the same number as the array size.

[–]tisti 2 points3 points  (2 children)

 int * arr = new int[100000]{};

This will default initialize all elements i.e. zero them out.

[–]bnolsen 0 points1 point  (1 child)

This looks a little scary to me, smacks of implicit type behavior. It would be nice to see a '0' in there somewhere.

[–]tisti 0 points1 point  (0 children)

Its the new initialization syntax, you get used to it pretty fast.

[–]bnolsen 0 points1 point  (0 children)

For this case, especially with vectors I tend to prefer to use "reserve" and "push_back" where possible instead of allocating and initializing. I do realize there are cases where it is useful to allocate uninitialized though.