A question about deque::clear()

Pages: 12
1
2
3
4
5
std::deque<int *> myList;
for(int i=0; i<100; i++) {
	myList.push_back(new int[1000]);
}
myList.clear();

For clear(), the reference said "All the elements in the deque container are dropped: their destructors are called...".

But I found that the memory blocks allocated by 'new int[1000]' were not freed, and I had to call delete[] to free them.

Is this normal?
Then, what does 'their destructors are called' in the reference mean?
Last edited on
Yes, this is normal. Note that you actually put a pointer into the deque. Its destructor is called, but destructor of a pointer does nothing. It cannot call delete, because not all pointers reference an object allocated with a new.

You need to free arrays manually, or use some kind of "smart pointer", that will do it for you in its destructor.
Last edited on
It's not normal, this is normal:

1
2
3
4
5
6
7
8
9
10
#include <deque>
#include <vector>
int main()
{
    std::deque<std::vector<int>> myList;
    for(int i=0; i<100; i++) {
        myList.emplace_back(1000); // or push_back(vector<int>(1000))
    }
    myList.clear();
}

http://ideone.com/E3cs5

There is no sensible reason for a deque of pointers to the first elements of new[]'d arrays to exist.
Are you saying C++ is retarded for providing both new[] and vector? If so, then I agree.
Are you saying C++ is retarded for providing both new[] and vector? If so, then I agree.


Indeed. Let's not give the user the choice. Let's simply force them to go with the slow option. [/sarcasm]
Last edited on
If you don't want to use vector you can at least use a smart pointer.
1
2
3
4
5
std::deque<std::unique_ptr<int[]>> myList;
for(int i=0; i<100; i++) {
	myList.emplace_back(new int[1000]);
}
myList.clear();
OK. But since new[] is not appropriate to create tables to be put into a deque, can you tell me when exactly it is appropriate?
Last edited on
new[] is appropriate when one wishes to create an array of something on the heap.
Is that so? Well, this is exactly the same as for std::vector.
A vector does many helpful things, such as take care of its own size, allow insertion of elements, answer questions about itself, provide easy ways to add elements to the front and the back, and many other such.

An array does none of these things. Hardly exactly the same.
Last edited on
Well, but that is my point - there is no reason to use an array allocated with new[]. std::vector provides the same functionality, and much more, and in general is better in every possible way.
Last edited on
Speed.
Speed of what? Creation, access? I doubt new[] is any more than marginally faster.
An array does none of these things

Which is why new[]'d arrays have no meaningful use (except in some rare cases when used with non-class types)
I regularly use them, particularly when I need to handle large numbers of huge images. I could use a vector, but as a general rule, I just don't need any of the extra bells n' whistles for that sort of thing.
closed account (o1vk4iN6)
An implementation of std::vector can use exceptions so it is slower, potentially.

This means C-style arrays are useless as well, so it should just be removed yes ? It wouldn't make sense if you couldn't allocate many objects using new if you can allocate one object.



I agree, do away with new[]! Abramus, can you get us started by implementing std::vector<T> without using new []? Remember, it has to be contiguous.
Thanks everyone!
Your replies gave me very useful information.
I guess I misundertood what 'destructor' in the reference of deque meant.

This is a different subject, but I'd like to ask you:
I do not prefer to use smart pointers, since it is quite complicated to me. I'm wondering what you guys think about using them. Do you think it's quite useful and efficient?
An implementation of std::vector can use exceptions so it is slower, potentially.
new[] also uses exceptions, in the sense that an exception will be raised when allocation fails. vector's operator[] makes unsafe access (no checks are made, no exceptions are raised) just as "standard" operator []. I don't see any differences here.

This means C-style arrays are useless as well, so it should just be removed yes ?
They are different, as they are allocated on the stack (when local), and they provide initialization lists. So their existence is justified.

Abramus, can you get us started by implementing std::vector<T> without using new []? Remember, it has to be contiguous.
Well, sure. All you need is malloc and placement new (without []). realloc would also help. Not only this is possible, this is actually how things are done. Even if new[] is used, it only allocates a raw array of bytes, i.e. malloc or any other C-like allocation function could be easily used instead. Otherwise vector would be terribly inefficient.

Look guys, maybe I was a little too offensive with my statement. We all know why new[] exists - at first there was no vector, and later nobody wanted to break backward compatibility. But from today perspective, new[] seems to be redundant. We could discuss if backward compability, or lack of built-in dynamic array type, is a good thing. But this would be probably too off-topic.
Last edited on
@trebari
I would say they are quite important in C++. Otherwise it will be hard to write exception-safe code. Their efficiency depends entirely on implementation, i.e. there are different kinds of smart pointers, with different functionality and efficiency.
Last edited on
Pages: 12