c++ - which new operator will be called new or new[]? -
in below program overloaded operator new []
getting called. if comment function overloaded operator new
getting called. shouldn't called default new []
operator?
#include <iostream> #include <stdlib.h> using namespace std; void *operator new (size_t os) { cout<<"size : "<<os<<endl; void *t; t=malloc(os); if (t==null) {} return (t); } //! comment below function void* operator new[](size_t size){ void* p; cout << "in overloaded new[]" << endl; p = malloc(size); cout << "size :" << size << endl; if(!p){ } return p; } void operator delete(void *ss) {free(ss);} int main () { int *t=new int[10]; delete t; }
looking @ the reference, see:
void* operator new ( std::size_t count );
called non-array new-expressions allocate storage required single object. […]
void* operator new[]( std::size_t count );
called array form ofnew[]
-expressions allocate storage required array (including possible new-expression overhead). the standard library implementation calls version (1)
thus, if overload version (1) not overload version (2), line
int *t = new int[10];
will call standard library's operator new []
. that, in turn calls operator new(size_t)
, have overloaded.
Comments
Post a Comment