6

I'm moving from Java to C++ and have really enjoyed it. One thing I don't enjoy is not understanding memory at all because Java used to do that for me.

I've purchased a book : Memory as a Programming Concept in C and C++ - Frantisek Franek

Are there some good sites for me to go and learn interactively about C/C++ and memory use (tutorials, forums, user groups)?

Stephano
  • 5,716
  • 7
  • 41
  • 57
  • This thread has a lot of useful info: http://stackoverflow.com/questions/395123/raii-and-smart-pointers-in-c – Manuel Feb 09 '10 at 19:52
  • 2
    Wow. Going from Java to C++ and enjoying it. I'm impressed. – JesperE Feb 09 '10 at 19:52
  • Well, you're putting emphasis on the right thing, since understanding resource management is (IMHO) absolutely critical to being a competent C++ developer. – Scott Smith Feb 09 '10 at 20:06
  • 1
    @JesperE Indeed, I was amazed myself. I should probably mention that I'm doing this for educational reasons and not for work. I think if my boss wanted me to suddenly write an app in C++ it would be much more frustrating than fun. – Stephano Feb 09 '10 at 20:42
  • I think "fascinated" is the term you're looking for, not "enjoying". ;-) – JesperE Feb 10 '10 at 07:03

2 Answers2

5

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

Most of the time don't dynamically allocate memory.
Use local variables (and normal member variables) and they will construct and destruct automatically.

When you do need pointers use a smart pointer.
Start with using boost::shared_pointer<T> instead of pointers.
This will get you on the correct path and stop accidently deleting memory at the wrong time and 90% of your code will release correctly (unfortunately cycles will cause a problem (in terms of leaks only) and you will need to design accordingly (but we have other smart pointers to deal with cycles weak_ptr))

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

Once you have the feeling start reading about the other smart pointers and when to use them:

Smart Pointers: Or who owns you baby?

Community
  • 1
  • 1
Martin York
  • 257,169
  • 86
  • 333
  • 562
  • +1, especially for "Most of the time don't dynamically allocate memory." – Fred Larson Feb 09 '10 at 19:58
  • @Martin - out of genuine interest: isn't using `shared_ptr` everywhere a bit slow? Would you recommend it, say, for a simulation or a videogame? – Manuel Feb 09 '10 at 20:07
  • 2
    What makes you think it is slow? But no I am not recomending it for everywhere. I am recomending it as a starting point for learning about smart pointers. Use the correct smart pointer to solve the problem at hand (but you have to start somwhere and smart pointers is a rather large topic to jump in). PS. I have no problems with smart pointers in simulators (correctness is king, optimize after you show it is bottleneck). – Martin York Feb 09 '10 at 20:12
2

Try these:

http://www.mycplus.com/tutorials/cplusplus-programming-tutorials/memory-management/

http://www.cantrip.org/wave12.html

http://linuxdevcenter.com/pub/a/linux/2003/05/08/cpp_mm-1.html

And in wikibook: http://en.wikibooks.org/wiki/C++_Programming/Memory_Management

This article will compare the Java memory management operators with the C++ equivalents: http://www.javamex.com/java_equivalents/memory_management.shtml

http://www.infosys.tuwien.ac.at/Staff/tom/Teaching/UniZH/CPP/slides/cpp_07.pdf

Hope these will help you!

anthares
  • 11,070
  • 4
  • 41
  • 61