Opinion on Memory Allocation
-
I started working with some source code where the original developers used pointers for almost every variable declaration, for instance. CRecordset* p = new CRecordset(); Is this a bad practice, besides the fact of possible memory leaks if it's not deleted? Thanks! Kurt
-
I started working with some source code where the original developers used pointers for almost every variable declaration, for instance. CRecordset* p = new CRecordset(); Is this a bad practice, besides the fact of possible memory leaks if it's not deleted? Thanks! Kurt
Recommended practice for modern C++ is to use pointers (i.e., explicit dynamic allocation) only when you have to. Otherwise declare an object on the stack or pass one by reference. It is almost certainly the case that in their code they are over-using pointers. These days I hardly ever explicitly allocate memory for pointers. When I do I use std::auto_ptr (though this is no good for pointers in collections - boost::shared_ptr helps out there or there are oher robust techniques). Other than that I use collection classes for data structures, e.g., MFC or STL. Kevin
-
I started working with some source code where the original developers used pointers for almost every variable declaration, for instance. CRecordset* p = new CRecordset(); Is this a bad practice, besides the fact of possible memory leaks if it's not deleted? Thanks! Kurt
Kurt Barlar wrote: Is this a bad practice, besides the fact of possible memory leaks if it's not deleted? Yes, it also looks like the dev came from a Java background and doesn't know how to use C++ the right way. If that's the case, I'd be worried about missing
delete
s. --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | 1ClickPicGrabber | CP SearchBar v2.0.2 | C++ Forum FAQ Strange things are afoot at the U+004B U+20DD -
Kurt Barlar wrote: Is this a bad practice, besides the fact of possible memory leaks if it's not deleted? Yes, it also looks like the dev came from a Java background and doesn't know how to use C++ the right way. If that's the case, I'd be worried about missing
delete
s. --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | 1ClickPicGrabber | CP SearchBar v2.0.2 | C++ Forum FAQ Strange things are afoot at the U+004B U+20DD -
I've yet to do maintenance programming on a C++ application which doesn't have some non-trivial memory management bugs. This is probably because so many C++ developers code in the style of 10 years ago.:| Kevin