Why is it needed to make a loop and delete every element of an array seperately, when you can do it all at once with delete database[] |
delete and delete[] do two different things.
delete takes one pointer which points to one object and deletes that one object.
delete[] takes one pointer which points to several objects (stored one after the other in memory) and deletes them all.
The reason you can't use delete[] here is because the objects are not stored consecutively in memory. Their
pointers might be stored consecutively in the vector, but the objects themselves are not. Remember we have multiple pointers here, but delete[] only works with a single pointer.
But really the short and easy answer is because you had multiple new's. Each new must have a matching delete, and each new[] must have a matching delete[]. Since you used new in a loop to allocate the objects, you must also use delete in a loop to free them.
Does database.clear() have the same meaning as = 0 for arrays and variables after deleting the allocated memory, |
Kinda sorta. clear() makes the pointers "go away" so they don't exist any more. It doesn't matter whether or not they're zero'd because their memory is freed and they no longer exist.
It's kind of like this:
1 2 3 4 5 6 7
|
void func()
{
int myvar;
// do a bunch of stuff with myvar
myvar = 0; // set myvar to 0 before the function exists because we're done with it
} // myvar "goes away" here
|
Zeroing myvar here is pointless because very shortly, myvar isn't going to exist. So it doesn't matter what its contents are.
It's the same thing with your vector:
1 2 3 4 5 6 7
|
for(unsigned i = 0; i < database.size(); ++i)
{
delete database[i];
database[i] = 0; // <- zeroing individual pointers here doesn't really do anything
}
database.clear(); // <- because once you clear, all those pointers no longer exist
|
The delete line, on the other hand,
is important because that does not delete the pointer, it deletes the object that the pointer points to (which is not contained in the vector, and thus doesn't go away when the vector is cleared).
I really need to get around to finishing that pointer tutorial I was working on.
Furthermore, the vector will be automatically cleared in its destructor. So you don't need to clear the vector if it will be going out of scope.
In both cases, though (pointer = 0 and database.clear()), it really doesn't hurt anything to do them. You could say it adds clarity to your code. So it isn't a BAD thing to do, it just isn't really necessary.
3. And for destructor, i just added this: |
That's perfect. That's all you need.
why do i get a warning: warning "C4018: '<' : signed/unsigned mismatch", when im not using un/signed in a for loops? |
vector::size returns an unsigned variable (type: size_t). int is signed. Comparing a signed and unsigned type gives you that warning on some compilers. If you want to avoid it, make i a size_t instead of an int.
I know what un/signed int means, for example, but not what just un/signed means. |
Signed variables' most significant bit have a negative weight. In english, this means that they can represent signed numbers, but in doing so they sacrifice the ability to represent higher numbers.
Unsigned variables can't represent signed numbers, but can represent higher numbers than their signed counterparts.
range of signed char: -128 .. 127
range of unsigned char: 0 .. 255
range of signed short: -32768 .. 32767
range of unsigned short: 0 .. 65536
etc
if you need types with dynamic memory the whole time your program is running, is it even neccessary to do delete allocated memory, since i presume when the program quits, it will no longer have to deal will memory, and therefore are the memory occupied by the program will be released, and therefore no memory release would be needed? |
You are correct. Memory will all be automatically freed en masse by the OS once your program exits. So if you are sure the lifetime of those objects will last the entire program then you don't HAVE to delete them. Some people would consider that bad practice though.
One side-effect of this is that the object's destructors will not run. So if you have the destructor doing something that would be outwardly visible (like generating a log file or something), that won't happen unless you actually delete the object.
(the same with clear() for vector) |
As previously mentioned, you never have to call clear() as part of cleanup. vector will do it automatically when it is destroyed.