Jump to content
  • Advertisement
Sign in to follow this  

how to delete content from deque ?

This topic is 3088 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

how to delete content from deque ? // deque::max_size #include <iostream> #include <deque> using namespace std; int main () { unsigned int i; deque<char *> d; cout << "Enter number of elements: "; i=255; char *h; int a; a=0;; while(a++<100){ h=new char[1024*1024*4]; sprintf(h,"item id=%i time=%i",a,clock()); d.push_back(h); _sleep(10); }; cout << "That size exceeds the limit.\n"; cout<<"deque maz size="<<d.max_size(); d.clear(); //wtf ? this is not working :| a=0;; cout<<"\n--size="<<d.size(); //ewen you see zero there content is still accessible and wastes system resources :teach: while(a++<100){ cout<<d[a]<<"\n"; }; system("pause"); return 0; }

Share this post


Link to post
Share on other sites
Advertisement
When you allocate pointers with new [], you must release them with delete [].

It is undefined behaviour to access the elements of a container after calling clear() on it. You should always use the containers iterators, or in the case of a random access container, a for loop bounded by the containers size() to access the elements.

Your program would be simplified by using more elements from the C++ standard library, such as std::string and std::stringstream:

#include <iostream>
#include <string>
#include <deque>
#include <sstream>
using namespace std;

int main ()
{
deque<string> d;

cout << "Enter number of elements: ";
int elements = 255;

for(int i = 0 ; i < elements ; ++i)
{
// string stream is like "cout"
// Except it writes to a string
stringstream stream;

stream << "item id=" << i << " time=" << clock();

// Here, we get the string from the stream and
// add it to our deque
d.push_back(stream.str());
_sleep(10);
};

cout << "That size exceeds the limit.\n";
cout << "deque maz size=" << d.max_size() << '\n';


d.clear();

cout << "--size=" << d.size();

system("pause");
return 0;
}


Note, even with this program (which cleans up 100% of allocations), if you attempted to access the elements you see the data, like before. However, this is the nature of undefined behaviour. What might be happening is that the allocator is holding onto the memory you have released, anticipating that you will request more (that is how applications typically proceed).

Finally, is there any particular reason you are using std::deque over, say, std::vector?

Share this post


Link to post
Share on other sites
Quote:
Original post by dragonnnnnnnnnnnnnn
is this means char ** arrays are not supported ?


They are supported, but using std::string over them can save you from a world of hurt (see [1], [2]).

Share this post


Link to post
Share on other sites
Quote:
Original post by rip-off
When you allocate pointers with new [], you must release them with delete [].

It is undefined behaviour to access the elements of a container after calling clear() on it. You should always use the containers iterators, or in the case of a random access container, a for loop bounded by the containers size() to access the elements.

Your program would be simplified by using more elements from the C++ standard library, such as std::string and std::stringstream:
*** Source Snippet Removed ***
Note, even with this program (which cleans up 100% of allocations), if you attempted to access the elements you see the data, like before. However, this is the nature of undefined behaviour. What might be happening is that the allocator is holding onto the memory you have released, anticipating that you will request more (that is how applications typically proceed).

Finally, is there any particular reason you are using std::deque over, say, std::vector?


well ok.
but all the data is still there.
i mean locked and in use by program.
check this code with process explorer and you will see it stiil hold memory locked for this program while it virtually uses zero memory !
#include <iostream>
#include <string>
#include <deque>
#include <sstream>
using namespace std;

int main ()
{
deque<string> d;

cout << "Enter number of elements: ";
int elements = 3000*1000;
for(int i = 0 ; i < elements ; ++i)
{
// string stream is like "cout"
// Except it writes to a string
stringstream stream;

stream << "item id=" << i << " time=" << clock();

// Here, we get the string from the stream and
// add it to our deque
d.push_back(stream.str());
// _sleep(10);
};

cout << "That size exceeds the limit.\n";
cout << "deque maz size=" << d.max_size() << '\n';





d.clear();

cout << "--size=" << d.size();





system("pause");
return 0;
}

Share this post


Link to post
Share on other sites
Virtual memory requested and committed by process has next to nothing to do with C++'s new and delete operators.

Short answer: process explorer shows very inaccurate numbers.

Share this post


Link to post
Share on other sites
Quote:
Original post by Antheus
Virtual memory requested and committed by process has next to nothing to do with C++'s new and delete operators.

Short answer: process explorer shows very inaccurate numbers.


i dont think so.
why would process explorer fail about that ?
its not even using c++ interface.
its using windows kernel messges afaik

Share this post


Link to post
Share on other sites
Quote:
Original post by dragonnnnnnnnnnnnnn
i dont think so.


You are free to think that fire isn't hot.

Quote:
why would process explorer fail about that ?
its using windows kernel messges afaik


Yes, and Windows (or any OS) kernel is a complex beast. Suffice to say that the way memory assigned to process (which is reported by OS tools) is something completely different from what C++ operators new or malloc do.

Aside from OS-related bookkeeping, memory mapped exe image, kernel resources, thread stacks and plethora of other bits and bytes, part of the memory used is also that claimed by your very application.


Quote:
its not even using c++ interface.
Which is why such tools will never be able to report memory usage accurately.

The only way to accurately track memory usage in C++ application is to manually track each allocation and deallocation. Tools such as valgrind are used for this purpose.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!