B
Brad
Before getting into the details, I'll start by saying that I've never
had need to use new and delete much before now and all the examples I
read about new/delete use types such as int and char. So if some of my
observations seem silly, that's probably why.
std::queue<std::string>* Q()
{
std::queue<std::string>* q = new std::queue<std::string>;
return q;
}
std::queue<std::string>* q = Q();
When I do q.push("a_string") memory is taken from the heap. q.push()
returns void. How can I tell if it fails due to lack of memory?
Also, it confuses me that while push() consumes memory that pop() does
not release it. The only way to release memory is to delete q. So if
one has more strings than memory that must go into the queue, then one
has to new, push(), pop() and then delete the queue. Repeat those
things over and over until all strings are processed. Is there a
better way?
I envisioned a container that consumed X amount of memory and could
push() and pop() (allocating and deallocating memory on the fly). So
that each time a pop() occurs a new push() could occur and the memory
would remain the same (provided the strings are the same length).
Any information or good reading material on this is much appreciated.
had need to use new and delete much before now and all the examples I
read about new/delete use types such as int and char. So if some of my
observations seem silly, that's probably why.
std::queue<std::string>* Q()
{
std::queue<std::string>* q = new std::queue<std::string>;
return q;
}
std::queue<std::string>* q = Q();
When I do q.push("a_string") memory is taken from the heap. q.push()
returns void. How can I tell if it fails due to lack of memory?
Also, it confuses me that while push() consumes memory that pop() does
not release it. The only way to release memory is to delete q. So if
one has more strings than memory that must go into the queue, then one
has to new, push(), pop() and then delete the queue. Repeat those
things over and over until all strings are processed. Is there a
better way?
I envisioned a container that consumed X amount of memory and could
push() and pop() (allocating and deallocating memory on the fly). So
that each time a pop() occurs a new push() could occur and the memory
would remain the same (provided the strings are the same length).
Any information or good reading material on this is much appreciated.