Skip to main content

c++, when should I use the stack or heap?

So I have started learning c++ recently, and as a .NET/Java developer I always want to write the following code.

var s = new myClass().

In c++ you have to manage memory yourself, there is no garbage collector.

If you do not use the new keyword var s = myClass() you will create that class and assign it to on the stack.

Any stack variables will be cleaned at the end of the block, so in this case s will be cleaned. However if you use var s = new myClass() s will be allocated onto the heap and must be deleted, otherwise memory leaks will occur.

To clean the variable you must call delete when you are done with the variable, this will cause the memory in the heap to be cleaned.

Continue Reading

Why I avoid switch statements in c++

So one thing that kills me a lot in c++ is the switch statement. As you all know switch statements look like the following.

auto s = 0;

   case 0:
   case 1:

Continue Reading

See all tags.