C ++, Seg Faults and Memory Management

I am moving from Java to C ++ and really enjoy it. The only thing I don't like is not understanding memory at all, because Java did for me for this.

I bought a book: "Memory as a Concept of Programming in C and C ++" - Frantisek Franek

Are there any good sites for me to go and get to know online about C / C ++ and memory usage (tutorials, forums, user groups)?

+6
c ++ c memory-management
source share
2 answers

Memory management is almost automatic in C ++ (with a few caveats).

In most cases, do not dynamically allocate memory.
Use local variables (and regular member variables), and they will automatically create and destroy.

When you need pointers, use a smart pointer.
Start by using boost :: shared_pointer <T> instead of pointers.
This will lead you to the right path and stop mistakenly deleting memory at the wrong time, and 90% of the code will be correctly issued (unfortunately, loops can cause a problem (only from the point of view of leaks), and you will need to develop accordingly (but we have other smart pointers for handling loops weak_ptr))

My fundamental rule is that a class never contains a RAW pointer. Always use some standard container or smart pointer. Using them; destructor calls become automatic.

As soon as you start reading other smart pointers and when to use them:

Smart pointers: or to whom does the child belong?

+5
source share

All Articles