Public Group

# how to delete content from deque ?

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

## 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 on other sites
In C++, we use std::string, instead of char*; std::stringstream, instead of printf; and we use [source][/source] tags when posting code.

Also, what do you expect d.clear() to do?

##### Share on other sites
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 on other sites
Quote:
 Original post by _fastcallIn C++, we use std::string, instead of char*; std::stringstream, instead of printf.

thans for reply but what it means ?
is this means char ** arrays are not supported ?

##### Share on other sites
Quote:
 Original post by dragonnnnnnnnnnnnnnis 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 on other sites
Quote:
 Original post by rip-offWhen 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 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 on other sites
Moved to For Beginners.

##### Share on other sites
Quote:
 Original post by AntheusVirtual 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 on other sites
Quote:
 Original post by dragonnnnnnnnnnnnnni 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.

1. 1
2. 2
JoeJ
18
3. 3
4. 4
frob
12
5. 5

• 13
• 16
• 13
• 20
• 13
• ### Forum Statistics

• Total Topics
632189
• Total Posts
3004653

×