Discussion:
how much memory does an empty STL deque occupy?
(too old to reply)
R.Z.
2005-09-20 09:38:15 UTC
Permalink
i was wondering whether it pays off in term of memory use to store many
empty deques, or is it better to only store deques with size > 0. and does
the size of an empty deque depends on the size of the type of its members
even if the deque is empty?
i've also read that the sizeof operator cannot be used with arrays that
allocate memory dynamically (and there is some dynamic memory allocation
inside of a deque class, isn't it?) so i figured it wouldn't give me correct
size of my deque.

best regards
Remi



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
msalters
2005-09-20 11:20:26 UTC
Permalink
I was wondering whether it pays off in term of memory use to store many
empty deques, or is it better to only store deques with size > 0. and does
the size of an empty deque depends on the size of the type of its members
even if the deque is empty?
Possibly. This will depend on the implementation, and I can think of
at least two sane implementations. One does depend, the other doesn't.
i've also read that the sizeof operator cannot be used with arrays that
allocate memory dynamically (and there is some dynamic memory allocation
inside of a deque class, isn't it?) so i figured it wouldn't give me correct
size of my deque.
True on all counts.

HTH,
Michiel Salters


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Ulrich Eckhardt
2005-09-20 14:01:02 UTC
Permalink
Post by R.Z.
i was wondering whether it pays off in term of memory use to store many
empty deques, or is it better to only store deques with size > 0. and does
the size of an empty deque depends on the size of the type of its members
even if the deque is empty?
If you wonder if holding a (null-)pointer to a container is more
memory-efficient than holding an empty container, the null pointer will
take less space. If you now compare the overhead of a one-element container
and compare it to a pointer to a one-element container, you will probably
loose, because just the single dynamic allocation of this container already
has lots of overhead.
I wouldn't worry about the useless storage taken by an empty container
unless I really know that I'm short on mem, have many containers and many
of them are empty most of the time. In that case though, I'd first do a
much deeper analysis if one couldn't restructure the whole thing (i.e.
optimizing memory use on the big level) to be more efficient instead of
doing micro-optimizations of memory use.
Post by R.Z.
i've also read that the sizeof operator cannot be used with arrays that
allocate memory dynamically (and there is some dynamic memory allocation
inside of a deque class, isn't it?) so i figured it wouldn't give me
correct size of my deque.
sizeof will always yield the size of the object itself, not that of other
memory perhaps referenced by it. When applied to a container, it will
always yield the same size, regardless of how many elements are stored in
the container.

Uli


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Loading...