Posts

Showing posts with the label swap

The Path to Swap, Part I: Automatic and Dynamic Variables

Dynamic memory has turned out to be both a blessing and a curse. I mean, unless you are doing something truly trivial, such as, say, a beginner C++ homework assignment, you are using it in one form or another. It seems that any language that has come out since Java has settled on using references and garbage collection. The question is, was this the right choice? To see why references and garbage collection are so popular, it helps to see what the standard alternative is. By that, I mean the model employed by C and C++. Note to people who care: I am aware of doing things like overriding new or using a different memory allocator to get garbage collection, or the use of autoptr and smartptr and all of those things. When I say the model used by C and C++, I mean the default one. In C++, there are two main types of variables, automatic and dynamic. An automatic variable is one that is created for you, and destroyed for you. You don't have to worry about it. Consider when you want to ...