You are correct Zlatomir.
I have never tried it before actually, because it never made any practical sense.
I just assumed it is possible.
Now I tried it, and indeed, the compiler complains.
Thanks for the heads up!
==========================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.
This is equivalent to:
The copy constructor kicks in here. Besides if you don't declare your own default constructor, copy constructor or assignment operator, the compiler will provide them for you.Qt Code:
Node t(n.GetChild(n.GetCount()-1));To copy to clipboard, switch view to plain text mode
No, it takes a const reference. That's two totally different things.
Last edited by wysota; 14th February 2011 at 19:01.
Thanks, I got it. The copy constructor was being called all the time. I got the difference now.
Humans make mistake because there is really NO patch for HUMAN STUPIDITY
One more thing on the topic though. Is it a good practice to avoid using pointers and use the stack space instead of heap ? That way there would be no memory leaks.
Humans make mistake because there is really NO patch for HUMAN STUPIDITY
No; that will quickly lead to convoluted code in all but the simplest applications. Pointers are fundamental to C/C++; there is no reason to avoid them.
If you're really worried about memory leaks, have a look at the STL auto_ptr class. Or run your code through a tool like valgrind to check for memory leaks, which is something you really ought to be doing anyway.
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.
Bookmarks