473,769 Members | 7,058 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how much memory does an empty STL deque occupy?

i was wondering whether it pays off in terms of memory use to maintain lots
of empty deques (it would be convenient for my algorithms but memory use is
more important). and does the size of a deque depends on the size of its
members even if the deque is empty? is there at all a way to check out how
much memory my deque occupies? i've read that the sizeof operator cannot be
used with dynamically allocated arrays so i figured it wouldn't give me
correct results with a deque (cause there is some dynamic memory allocation
going on there, isn't it?)

best regards
remi
Sep 20 '05 #1
9 3734
to further clarify what i mean:
i have a deque of deques and i am wondering whether to keep the number of
the deques in a deque constant (cause it would make my code simpler) or vary
it by removing any empty member deques (to conserve memory).
Sep 20 '05 #2
R.Z. wrote:
i was wondering whether it pays off in terms of memory use to
maintain lots of empty deques (it would be convenient for my
algorithms but memory use is more important).
That can't be answered without a lot more information.
and does the size of a
deque depends on the size of its members even if the deque is empty?
is there at all a way to check out how much memory my deque occupies?
i've read that the sizeof operator cannot be used with dynamically
allocated arrays so i figured it wouldn't give me correct results
with a deque (cause there is some dynamic memory allocation going on
there, isn't it?)


Using sizeof will tell you the bare minimum bytes that a deque occupies.
This value will not change regardless of the number of items in the deque.
It is also unlikely to be affected by the item size, though I think you'd
have to test that on the compiler to be used.
As an example, in VS .NET 2003 an empty deque appears to occupy 20 bytes
always, not counting its dynamic allocations. As for the dynamic
allocations, you would hope that it wouldn't do any until you added the
first item to tthe deque, but I think you'd have to examine the deque's data
members or its code to be sure. AFAIK, a given implementation of the
standard library could implement such details of the deque differently from
another implementation.

DW
Sep 20 '05 #3
to further clarify what i mean:
i have a deque of deques (some of which are or may become empty during
program execution). i wonder whether i should keep only non-empty deques (to
conserve memory) or maintain all of them (to simplify coding).
Sep 20 '05 #4
R.Z. wrote:
i was wondering whether it pays off in terms of memory use to maintain lots
of empty deques (it would be convenient for my algorithms but memory use is
more important). and does the size of a deque depends on the size of its
members even if the deque is empty?
It might, it might also depend on how the deque has been used upto that
point.

is there at all a way to check out how much memory my deque occupies?
No, other then examining the code that is used in your implementation
and so working it out for yourself.

i've read that the sizeof operator cannot be used with dynamically allocated arrays so i figured it wouldn't give me
correct results with a deque (cause there is some dynamic memory allocation
going on there, isn't it?)


Right.

You might consider the swap trick, assuming you have a deque of type T
called mydeque.

deque<T>().swap (mydeque);

this creates a temporary empty deque and swaps it with mydeque. This
result is that mydeque becaome exactly like a newly constructed empty
deque. There are still no guarantees but I would say that after doing
this it is more likely that mydeque is occupying the minimum possible
amount of memory.

Josuttis in 'The C++ Standard Library' advises to use a deque when 'It
is important that the container frees memory when it is no longer used
(however the standard does not guarantee that this happens)'. I would
read this as implying that typically the size of an empty deque would
not depend on the size of its members, but of course there are no
guarantees.

john
Sep 20 '05 #5
R.Z. wrote:
i was wondering whether it pays off in terms of memory use to maintain lots
of empty deques (it would be convenient for my algorithms but memory use is
more important). and does the size of a deque depends on the size of its
members even if the deque is empty? is there at all a way to check out how
much memory my deque occupies? i've read that the sizeof operator cannot be
used with dynamically allocated arrays so i figured it wouldn't give me
correct results with a deque (cause there is some dynamic memory allocation
going on there, isn't it?)

best regards
remi


This is all implementation dependent and I've seen different library
implementations do things in different ways. What you can do is use
sizeof to find the size of the non-dynamically allocated portion of the
container. And then you can use a simple allocator that reports all
memory allocations to observe the dynamic allocations. Here's one such
allocator:

http://www.josuttis.com/libbook/memory/myalloc.hpp.html

In my experience, some containers will allocate space for one element at
a time while others will never allocate space for less than a certain
number of elements at a time (32 is a common value). From what I've
seen, STLPort does one at a time and so does SGI, RogueWave does 32 but
this is controlled by a preprocessor defn. so can be altered. I've
never seen an implementation that dynamically allocates memory while the
container is still empty. (But of course one could.)

Mark
Sep 20 '05 #6
R.Z. wrote:
i was wondering whether it pays off in terms of memory use to maintain lots
of empty deques (it would be convenient for my algorithms but memory use is
more important). and does the size of a deque depends on the size of its
members even if the deque is empty? is there at all a way to check out how
much memory my deque occupies? i've read that the sizeof operator cannot be
used with dynamically allocated arrays so i figured it wouldn't give me
correct results with a deque (cause there is some dynamic memory allocation
going on there, isn't it?)

best regards
remi

The only way to be sure of how deque memory management works is to
provide your own allocator. I tend to use vector which has a reserve
member function that allocates memory for a specified number of items
thus avoiding reallocation later. Naturally this is only useful if you
know in advance the algorithms requirements. Certainly for an algorithm
I developed it did speed thing up, although reducing loop counts by only
considering relevant data was most effective, (this required the data
to be sorted first). Unfortunately this is not always applicable and
is somewhat complex to implement, (not good if you're short of time).

Hope you find this useful

JFJB
Sep 20 '05 #7
"n2xssvv g02gfr12930" wrote:
R.Z. wrote:
i was wondering whether it pays off in terms of memory use to maintain lots
of empty deques (it would be convenient for my algorithms but memory use is
more important). and does the size of a deque depends on the size of its
members even if the deque is empty? is there at all a way to check out how
much memory my deque occupies? i've read that the sizeof operator cannot be
used with dynamically allocated arrays so i figured it wouldn't give me
correct results with a deque (cause there is some dynamic memory allocation
going on there, isn't it?)

best regards
remi

The only way to be sure of how deque memory management works is to
provide your own allocator. I tend to use vector which has a reserve
member function that allocates memory for a specified number of items
thus avoiding reallocation later. Naturally this is only useful if you
know in advance the algorithms requirements. Certainly for an algorithm
I developed it did speed thing up, although reducing loop counts by only
considering relevant data was most effective, (this required the data
to be sorted first). Unfortunately this is not always applicable and
is somewhat complex to implement, (not good if you're short of time).


Perhaps the following idea is a bit far removed from the OP's topic,
but perhaps hir (and others) may find it useful:

If one wants to "consider only relevant data" as "n2xssvv g02gfr12930"
(how do you pronounce that, anyway?) says, one way is by using a
std::multimap<> instead of a std::deque<>.

Consider my quandry when writing a duplicate-file remover. Consider
a directory with 1000 files in it. Doing a byte-by-byte comparison
on all possible file pairs involves 1000! comparisons (where "!" means
factorial). This is about 4.0 x 10^2567. Even at 100 file compares
per second, this will take 1.3 x 10^2560 years. (By comparison, the
entire duration of the universe, so far, is only about 10^10 years.)
Obviously not workable! (Well, obvious NOW. At the time, I was
wondering why the program was siting there for hours hogging 100%
CPU time. When I realized the transuniversal enormity of my blunder,
I was suitably embarrassed.)

So, instead of using a std::deque<>, std::vector<>, or std::list<>,
I used a std::multimap<i nt, FileRecord> (where FileRecord is a struct
with file info like name, size, date, etc.). All files are then
automatically sorted into "equivalenc e groups" keyed by size. Since
only files the same size can be duplicates, this cuts the time of
execution from googol^25 years down to about 2 seconds, because the
app now only compares maybe 2 or 3 pairs of files instead of 1000!.
A significant improvement in efficiency. :-)

--
Cheers,
Robbie Hatley
Tustin, CA, USA
email: lonewolfintj at pacbell dot net
web: home dot pacbell dot net slant earnur slant
Sep 20 '05 #8
Robbie Hatley wrote:

Consider my quandry when writing a duplicate-file remover. Consider
a directory with 1000 files in it. Doing a byte-by-byte comparison
on all possible file pairs involves 1000! comparisons (where "!" means
factorial).


Probably I'm misunderstandin g what you did, but how do you get 1000!
pairs from 1000 files. There are 1000C2 = 1000*999/2 ways to choose a
pair from 1000 items. This is vastly less than 1000!

Mark
Sep 20 '05 #9
"Mark P" wrote:
Robbie Hatley wrote:

Consider my quandry when writing a duplicate-file remover. Consider
a directory with 1000 files in it. Doing a byte-by-byte comparison
on all possible file pairs involves 1000! comparisons (where "!" means
factorial).


Probably I'm misunderstandin g what you did, but how do you get 1000!
pairs from 1000 files. There are 1000C2 = 1000*999/2 ways to choose a
pair from 1000 items. This is vastly less than 1000!


My mistake. For each member of the sequence, I'm comparing
it to all files to it's right. So the total number of compares is
SUM(999, 998, ... , 3, 2, 1) not PRODUCT(999, 998, ... , 3, 2, 1).
I don't know why my mind came up with the latter. Twas late
when I wrote the post, and my mind must have been fuzzier than
I thought.

Still, though, comparing files that way, just iterating through
a list or deque of 1000 FileRecord objects with nested for loops,
means 499500 separate file comparisons. Takes a long time, even
if you don't actually do byte-compares on files with non-equal
sizes, because you still have to stop and compare sizes of each
possible pair of file records.

Whereas with std::multimap, typically you get 0 or 1 or 2 file
comparisons in a directory with 1000 files. Takes a second or so.
Huge difference. That's the advantage of "considerin g only the
relevant data" as one of the other posters mentioned. multimap
makes that easy by allowing one to group things into "equivalenc e
groups". That way you can visit just those keys that have more
than one value, and iterate through those equivalence groups using
"lower_boun d" and "upper_boun d".

Also, it further speeds things up if you make a list L of "relevant"
keys while loading a multimap M. Then iterate through L with
iterator i, looking only at equivalence groups in M keyed by (*i).

--
Cheers,
Robbie Hatley
Tustin, CA, USA
email: lonewolfintj at pacbell dot net
web: home dot pacbell dot net slant earnur slant
Sep 20 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1535
by: pmastroianni | last post by:
When a class is instantiated, will the ctor and dtor occupy memory? IF ctor and dtor do occupy memory of an instantiated class where would they be in memeory?
12
1722
by: Michael | last post by:
In the below example, I beleive that the char array buffer should only occupy 40 bytes of the stack within the inner braces. However the compiler seems to use 40 bytes of the stack througout the entire function that this example is cut from. Is this normal? if(midnightflag==1) { lcd_clear(); lcd_locate(1,1);
13
6163
by: hurry | last post by:
In order to avoid declaring static variables in a function I was asked to write a scratch memory. Reserve a block of memory outside the function and assigning pointers to the memory locations as per convenience and access them. I was told this would save some memory. I dont understand the logic behind this, as i`ve declared variables as global (assuming i`ve declared the block in main() ) this would always b a residual data for access at...
14
2732
by: MKoool | last post by:
I have an application with one function called "compute", which given a filename, goes through that file and performs various statistical analyses. It uses arrays extensively and loops alot. it prints the results of it's statistical significance tests to standard out. Since the compute function returns and I think no variables of global scope are being used, I would think that when it does, all memory returns back to the operating...
4
438
by: mast2as | last post by:
Hi guys I wrote a short program to test a Tree class... I plan to use it a lot in my application so thought I would do a memory leak check before I start using it. The code seems good to me in a glance (new & delete seem to be called for all dynamically crated objects) but when I run valgrind, valgrind reports a memory leak. The problem seems to come when i start using push(). Could anyone help me with this please ? I just don't see...
4
4821
by: mast2as | last post by:
Hi again I was still debugging some code and check for memory leaks with valgrind and found out that valgrind finds a leak when i use deque<Somethingaqueue ?! I am compiling under Linux for example: #include <deque> using namespace std;
9
3503
by: Randy Yates | last post by:
Hi Folks, This may be somewhat off-topic, but it sorta fits. This is a C++ application that will be built using gcc and targeted (via the wonderful wxWidgets library) for both Windoze and Linux. Let's say I want to maintain an array of 16-bit integers that can be 100's of MBs in length. What's the best way to do this? Should I simply create the array and let the OS deal with swapping? Or should I
2
5307
by: tikcireviva | last post by:
Hi Guys, I've done a mulithread queue implementation on stl<queue>, my developement environment is on VC6 as well as FC3. Let's talks about the win32 side. The suspected memory leak is find after I've run through my unit test cases. Test Case:
1
2375
by: katem | last post by:
Here's my problem: I have a class A that uses new to create objects of class B and push them onto a deque, like this (code stripped down for clarity): void ImageOperations::FormMask() { std::deque<BoundingBox*> boundBoxDeque; if (condition met) { BoundingBox pBox = new BoundingBox(x,y,z); if (second condition met) {
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10049
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9997
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9865
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3965
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3565
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.