Showing posts with label differences. Show all posts
Showing posts with label differences. Show all posts

Free New, Delete Malloc

On numerous ocassions have I found people making mistakes in usage of the memory allocation routines acceptable to C++. Primarily between new, delete and malloc, free (and not to mention the array forms of new and delete).

A mistake can be very dangerous from a memory leak to undefined behavior that may make your computer dissappear! :D Of course, I am kidding but it is still dangerous.

Take a look at this FAQ entry that I posted on Codeguru regarding the topic - C++ Memory Management : What is the difference between malloc/free and new/delete?

Hope you find it useful.

Take care!

C++ or ++C (read i++ or ++i)

Pre-increment and post-increment : achieve "almost" the same end result with a little difference. It is always adviced to use the pre-increment form in expressions where you do not care what value the expression ought to use - the new incremented one or the older value prior to increment. Also when it matters, if you need the former then use the pre-increment form and if the latter use the post-increment form. That is the basic difference.

The post increment form has a drawback of generating a temporary (yeah, that can be optimized by the optimizer but still).

To understand the difference and when to use which form, you may find this Codeguru FAQ entry quite helping : C++ Operator: Why should I use '++i' instead of 'i++'?

Have fun!

Check out this stream