Re: pointer in queue
On 3 Feb., 02:09, Carl Forsman <fatwallet...@yahoo.com> wrote:
On Mon, 02 Feb 2009 19:44:28 GMT, Juha Nieminen wrote:
Carl Forsman wrote:
std::deque < Sock* > * socks;
socks = new deque< Sock* >;
Are you, by any chance, a Java programmer?
yes i know more java than c++
Then do something about it. Learn C++. The best way to do this is with
a good book next to you. The only reason I'm writing more than "grab a
book" this time is that you still don't seem to acknowledge that the
programming styles (C++ compared to Java) are any different.
In C++ you need a corresponding _delete_ for every _new_. Try to avoid
new/delete as much as possible. In C++ you have a choice where to
create an object (free store versus automatic storage). Prefer
automatic storage and real class members over free store and pointers.
Don't expect the standard containers to clean up after you if you put
("stupid") pointers into them. They only manage the pointers. When you
"destruct" normal pointers the memory area they point to is not
magically deallocated. What the pointers refer to is of no concern to
the standard containers.
Homework assignments:
- Learn about RAII (resource acquisition is initialization).
- Learn about memory management
- Learn about Handle idiom and related idioms.
Cheers!
SG