How can you map the differences between Javascript objects?

What are the differences between new/delete and malloc/free when allocating memory for objects?

  • This is sort of a follow up for http://stackoverflow.com/questions/240212/what-is-the-difference-between-new-delete-and-malloc-free This link says that new and delete call constructor/destructors respectively.Can malloc/delete be used to allocate memory for objects? Will they call the constructor/destructor ? Also it was mentioned that reallocation isn't handled by new but done by malloc due to copy constructors.Can someone shed some light on this by some example code? EDIT : I guess I should add the reason I asked this question.I tried using malloc/free for objects in gcc and apparently it works but doesn't call the constructor and destructor.

  • Answer:

    The main difference between new and malloc is that new invokes the object's constructor and the corresponding call to delete invokes the object's destructor. There are other differences: new is type-safe, malloc returns objects of type void* new throws an exception on error, malloc returns NULL and sets errno new is an operator and can be overloaded, malloc is a function and cannot be overloaded new[], which allocates arrays, is more intuitive and type-safe than malloc malloc-derived allocations can be resized via realloc, new-derived allocations cannot be resized malloc can allocate an N-byte chunk of memory, new must be asked to allocate an array of, say, char types Looking at the differences, a summary is malloc is C-esque, new is C++-esque. Use the one that feels right for your code base. Although it is legal for new and malloc to be implemented using different memory allocation algorithms, on most systems new is internally implemented using malloc, yielding no system-level difference.

Robert Love at Quora Visit the source

Was this solution helpful to you?

Other answers

new calls the object constructor (even primitive types will get initialized to 0 etc.) malloc() only allocates raw memory. With placement new, you can cause new to execute a constructor without allocating memory. This is very useful In most library implementations new actually calls malloc()

Vivek Nagarajan

new and delete are C++ specific features. They didn't exist in C. malloc is the old school C way to do things. Most of the time, you won't need to use it in C++. malloc allocates uninitialized memory. The allocated memory has to be released with free. calloc is like malloc but initializes the allocated memory with a constant (0). It needs to be freed with free. new initializes the allocated memory by calling the constructor (if it's an object). Memory allocated with new should be released with delete  (which in turn calls the destructor). It does not need you to manually  specify the size you need and cast it to the appropriate type. Thus,  it's more modern and less prone to errors.

Amar Saurabh

This question doesn't seem to ask any questions that I don't answer in . But I'll reiterate my previous points as raised here. Can malloc/delete be used to allocate memory for objects? Will they call the constructor/destructor? You cannot use malloc to allocate C++ objects and you cannot use free to deallocate C++ objects as they do not call the constructor or destructor. I can imagine someone making it work in a very hack-y and platform-specific manner, but it would be neither portable nor standards compliant. Nor could I imagine why you'd want to. Just use new and delete. Moreover, you cannot manually call the constructor or destructor on an object instance. What about realloc? There is no equivalent to realloc for new and delete. Mostly because this doesn't make sense. If you allocate a Foo object, what exactly would you reallocate? There is only one of them. You can use copy constructors to copy and move things around, but that isn't addressing the same need as realloc. Realloc is useful when you have a raw C-style buffer of N bytes and later realize you want it to be M>N bytes. For example: // allocate 512K char* buf = malloc (512 * 1024); if (!buf) // error ... And then, later: // change our mind, we want 768K ... char* new_buf = realloc(buf, 768 * 1024); if (!new_buf) // error ... If you need to do this in C++, you can: Just use malloc/free/realloc for this allocation instead of new and delete. But for allocating your C++ objects? Use new and delete.

Robert Love

'new' is only available in C++.  malloc() is available in both C and C++. Memory allocated via 'new' is not relocatable. In other words, there is no realloc() equivalent. The return value of 'new' is a typed pointer.  The return value of malloc() is a void*. The system may implement 'new' and malloc() using different heaps.

Jon Parise

When you do className classObj = new className; It invokes the 'operator new'. The default operator new does two things: Allocate heap memory for the object Calls the constructor to instantiate the object Now malloc is a function that operator new can use in it's 1st step, to allocate heap memory. Malloc returns a void* to a block of memory. That's all it does. And in the 2nd step of operator new it initializes the object in this allocated block of memory. This is the default behavior of operator new. But you can change this behavior of operator new to do anything you want (operator overloading). One of the best examples of overloaded new is placement new where you can pass a pointer to a pre-allocated block of memory (memory you have obtained by calling malloc explicitly). In this case operator new skips step #1 and uses the passed block of memory to instantiate a new object by calling the constructor. Note that operator new is the only way you can call a constructor (well except is constructors themselves (delegating constructors))..

Chan Liyanage

new is an operator, malloc is a function new "operates" on classes, malloc takes a number of bytes new can be overridden on a per-class basis, malloc cannot

Alex Gaynor

Maybe an example... void *p =malloc(sizeof (T)); T*t =new (p)T; //placement new only calls ctor using existing memory /// use t T->~T();//call dtor free(p); What "regular"  new/ delete does is combine the first two and last two operations together respectively, in such a way that if the ctor throws, no memory is leaked. malloc and free aren't necessarily used, any heap manager will work

Lance Diduck

new typically uses malloc to do its work. malloc does one thing: it allocates memory, through means depending on the platform (brk, mmap, VirtualAlloc, etc.) and offers it to the user. new is a bit more complex. In its default mode it allocates memory using malloc and if that works ok it calls the constructor of the class you're instantiating. If that constructor fails (aka throws an exception), it will free the memory. Now, that's not the whole story. There is also a form of new that doesn't allocate memory but, instead, it uses preallocated memory. This is called 'placement new' and it's used to implement efficient memory pools. However, calling delete on pointers allocated with placement new is forbidden (as they might delete something else). To call this form of new one uses the following form: char *buffer = new char[1024]; X *ptr = new(buffer) X; // ptr will have the same value as buffer. ... // do stuff ... // don't call delete ptr delete[] buffer; Of course, placement new is incompatible with most pointer wrapper classes from STL.

Dorin Lazăr

Following are the differences between malloc() and operator new. 1) new calls constructors, while malloc() does not. 2) new is an operator, while malloc() is a function. 3) new returns exact data type, while malloc() returns void *. 4) operator new can be overloaded, malloc cannot be overloaded. 5) operator new throws an exception if there is not enough memory, malloc returns a NULL. 6) New Memory allocated from 'Free Store' and malloc allocate memory from HEAP. 7) Operator new initializes memory to zero but malloc initialize with garbage values.

Prashant Gupta

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.