In addition to what has been said, the stack size is usually quite limitted, so any realy world application might soon run out of memory if all you use is the stack.
In addition to what has been said, the stack size is usually quite limitted, so any realy world application might soon run out of memory if all you use is the stack.
==========================signature=============== ==================
S.O.L.I.D principles (use them!):
https://en.wikipedia.org/wiki/SOLID_...iented_design)
Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.
Just a little addition to what has been said already: don't use auto_ptr it has some usage restriction (like you can't have an container of auto_ptr) and if i remember correctly it is on removal list of the new standard, so you will have to re-write code when you will upgrade to a C++0x compiler.
Instead you can use boost shared_ptr (it should be included in tr1:: namespace it will be in std:: in C++0x) or Qt has QSharedPointer
But anyway you need to understand pointers and memory management, even if Qt simplifies things (see parent-child relationship) you still need to know what is deleted by parent and what you should delete yourself.
Is there a way to find out how much memory is the stack limited to. I'm just writing a prototype and yes memory is a issue. But I would only need around 200-300 mb max. I'm trying to import large text files.In addition to what has been said, the stack size is usually quite limitted, so any realy world application might soon run out of memory if all you use is the stack.
Humans make mistake because there is really NO patch for HUMAN STUPIDITY
On Linux, you can say 'ulimit -a'; one of the numbers reported is stack size. On my machine, it is currently set at ~8MB, which is fairly typical.
Which space does STL containers like vector or hash_map use ?
For example:
Qt Code:
int main(int argc,char **argv){ vector<string> v; string s = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; //sizeof(s) = 8 for(unsigned long i = 0;i< 18446744073709551615; i++) v.push_back(s); return 0; }To copy to clipboard, switch view to plain text mode
Isn't the vector being created on the stack ? Why doesn't this program crash even when the size goes way past stack limit (~8 MB) .
Last edited by ct; 16th February 2011 at 16:26.
Humans make mistake because there is really NO patch for HUMAN STUPIDITY
The vector object yes, the elements no.Isn't the vector being created on the stack ?
Think about it: since the elements are being dynamically added, how could they be added on the stack?
That would mean that the vector has a predefined size.
==========================signature=============== ==================
S.O.L.I.D principles (use them!):
https://en.wikipedia.org/wiki/SOLID_...iented_design)
Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.
But we don't need to deallocate the memory of vector or other STL containers ? Does it mean that as soon as the object created on the stack runs out of scope, it's memory on heap is deallocated automatically ?
If it does clean up, then I should be ok. I have designed the class such that instead of dynamically allocating objects, i'm inserting it in the vector so that when it runs out of scope, memory will be freed.
Humans make mistake because there is really NO patch for HUMAN STUPIDITY
If you use the container like this:But we don't need to deallocate the memory of vector or other STL containers ?
you don't need to de-allocate anything, vector will remove all of it's elements when it runs out of scope. But if you store pointers:Qt Code:
std::vector<MyClass> v; v.push_back( MyClass() ); //...To copy to clipboard, switch view to plain text mode
then you need to remove the objects yourself. When vector runs out of scope it still removes all of it's elements, but this time no ~MyClass destructor is called - since it's just a pointer that is removed - and if you don't do proper cleanup yourself, you'll have a memory leak.Qt Code:
std::vector<MyClass*> v; v.push_back( new MyClass() ); //...To copy to clipboard, switch view to plain text mode
Does it mean that as soon as the object created on the stack runs out of scope, it's memory on heap is deallocated automatically ?... but a.b (object created on heap) will not be removed automatically, you need to call "delete b" yourself.Qt Code:
class A{ public: A(){ b = new B(); } ~A(){ } B * b; }; //... { A a; } // a (created on stack) goes out of scope here...To copy to clipboard, switch view to plain text mode
Last edited by stampede; 17th February 2011 at 08:48. Reason: to many spaces in code
Bookmarks