I have used C up to now, just starting to teach myself C++. Are there any advantages to using new/delete over malloc/free besides less typing?
Are they essentially the same thing, just with different syntaxes?
I have used C up to now, just starting to teach myself C++. Are there any advantages to using new/delete over malloc/free besides less typing?
Are they essentially the same thing, just with different syntaxes?
You mean aside from the fact that new/delete calls the constructor/destructor for the objects it creates, which malloc/free doesn't. Sure, beside that fact, they are pretty much the same. They both allocate memory and assign it to the pointer you give, and release the memory from a given pointer.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
They are very different in that malloc/free just gives you a bunch of bytes, whereas new/delete construct and destruct allocated objects. That is, you can't use malloc/free for anything that needs to have its constructor/destructor called (unless you want to go through great trouble).
Furthermore, in C++ it is almost always possible to avoid the array form of new[]/delete[], since you can just use std::vector. It is also possible to avoid explicit calls to delete, if you let smart pointers like boost::shared_ptr take care of new'ed objects.
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
I guess I should have been more specific - I am interested in arrays of basic types: for example
is
functionally the same asCode:array = new int[10]
?Code:array = (int *) malloc(10 * sizeof(int))
Not quite. If malloc was unable to allocate memory, it would return a null pointer, but the corresponding behaviour from new[] would be to throw std::bad_alloc.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
As Laserlight points out, new and malloc behaves differently when there isn't enough memory to satisfy the request. However, the other part of the question is sort of "what should I use", which I would say is:
1. If you are writing C, you haven't got a choice: malloc/free it is.
2. If you are writing C++, use new/delete - it will should never be wrong to do this, and if for some reason you change your mind, and decide to change the array of integers into a class CInteger, you do not need to make as many changes.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
There is also more type-safety.
This is legal (but wrong) in C:
float* x = malloc(sizeof(int));
On the contrary, if you do it in C++, you will get a compile error:
float* x = new int;
Type-safety is one of C++'s most powerful features. Make good use of it.