-1

Possible Duplicate:
Difference between 'new operator' and 'operator new'?

What is the difference between using new and operator new ?
I read somewhere a while ago that operator new behaves like malloc(no constructor). Is this correct? If so can someone show me on example, when trying to allocate an object using "operator new" it gives me error.
Thanks.

Community
  • 1
  • 1
Adrian
  • 19,440
  • 34
  • 112
  • 219
  • 1
    http://stackoverflow.com/questions/1885849/difference-between-new-operator-and-operator-new – James Mar 16 '11 at 19:09
  • You shouldn't be calling operator new. If you want to control how memory is allocated for your object, implement operator new, then call ordinary new. That will automatically invoke your version. – Dawson Mar 16 '11 at 19:10
  • search is your friend. This question has already been asked and answered with an almost identical question title. – wheaties Mar 16 '11 at 19:11
  • @wheaties: i search in the helper threads that appeared after enter title, i didnt saw anything related, sorry. – Adrian Mar 16 '11 at 19:13

2 Answers2

2

operator new is the lowest level allocation mechanism. Actual objects should be allocated with new, which tells C++ to actually create the object and set up all its necessary plumbing (superclass information, vtables, etc.), without which it isn't capable of being an object.

geekosaur
  • 59,309
  • 11
  • 123
  • 114
2
  • operator new doesn't call constructor of class T, and returns void*, not T*.
  • new internally calls operator new, then call the constructor of the class T to construct the object in the allocated memory and then return T*.
Nawaz
  • 353,942
  • 115
  • 666
  • 851