Clear Vector ?

Hello is it necessary to clean a string vector at the beginning of a program or is the vector cleaned at each execution of the program?
strings are set to empty string upon initialization, every time you run the program.
some things are not; an empty array or a grabbed memory block could be the same as last run (not reliable! do not write anything assuming it will persist!).

Edit: my compiler appears to clear vectors for you too. I did not realize it had this inefficiency.
Last edited on
Elements of a vector<int> become 0, not junk, when you do the above syntax.
Edit: wait maybe this changed in c++11... Checking around.

Edit 2: jonnin cleared his example so my post is no longer applicable
Last edited on
Every STL conatiner, but std::array is empty when created.
jonnin, regarding your example of std::vector<int>(100), I actually am not certain to whether or not it is initialized. Maybe an expert can speak to it.

On cppref, it shows
1
2
3
4
5
6
explicit vector( size_type count,
                 const T& value = T(),
                 const Allocator& alloc = Allocator());     (until C++11)
         vector( size_type count,
                 const T& value,
                 const Allocator& alloc = Allocator());     (since C++11)

The pre-C++11 declaration to me shows that it initializes it with T(), which in this case would be int(), which I believe is zero-initialization.

But post-C++11, it would use
1
2
3
4
5
6
explicit vector( size_type count );
(since C++11)
(until C++14)

explicit vector( size_type count, const Allocator& alloc = Allocator() );
(since C++14)


So, I think it depends on the default Allocator (but I would assume a default allocator would leave the memory untouched, so... I don't know).
Last edited on
std::vector always default-constructs elements allocated by the std::vector constructor or by resize(), when a value parameter is not provided. In the case of integers and floats, this means zero-filling. An implementation that doesn't do this is incorrect.
Last edited on
Thank you.
Thanks; I have not spent a lot of time trying to break the STL containers and was unsure. I thought it would just use a new statement and keep it as quick as possible. Sorry for the edit, I didn't want to be confusing so I took the wrong info back out.
Last edited on
Hello, thank you for all these details. So I'm going to do a cleanup at the end of the execution to make sure. Thanks again.
@codingday, what does it mean to "clean" a vector?
codingday, show us an actual example of what you're talking about.

All memory allocated by the program will be released back to the OS at the end of the program, although in general it is best practice to delete anything you new, especially for any long-running program, because you don't want memory leaks.

But that's beside the point, because if you're just doing something simple like vector<int>, and not actually using new/delete yourself, all memory will be automatically cleaned up when the vector<int> goes out of scope (for example, at the end of main). So you don't need to worry about it.
Last edited on
setting everything to zero at the end of the program is just wasting time.
if a program is getting stale memory values out of ram from the execution of previous programs (including itself), the problem needs to be fixed on the FRONT END so that does not happen, not on the back end by trying to zero out ram. If all the programs are doing things correctly, there is never any need to do this (exception, extremely low level, no-OS running, hardware may have this kind of need). If you need to do this for a weird system, it would be documented with bold, underlined, blocked out text warning you to make it so.
Last edited on
it would be documented with bold, underlined, blocked out text warning you to make it so.
Customer would still ignore it ;)
One time I was writing a password generation program. I'm not sure how I managed it, but I was getting strings from process names that had run previously. "Studio" was a frequent substring in my passwords. This was as late as the Windows 7 days.
Sounds like you used the output from tasklist as a source of entropy.

It's also worth noting that while the program's memory will be cleaned up by the OS, stuff allocated in DLL calls might still contain information from the previous DLL call, e.g. the characters in a global char array in the DLL's logic.
Last edited on
Topic archived. No new replies allowed.