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:

  1. void* operator new ( std::size_t count );
    called non-array new-expressions allocate storage required single object. […]

  2. void* operator new[]( std::size_t count );
    called array form of new[]-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

Popular posts from this blog

php - How to add and update images or image url in Volusion using Volusion API -

javascript - jQuery UI Splitter/Resizable for unlimited amount of columns -

javascript - IE9 error '$'is not defined -