   Overloading new[] to malloc?
  In a nutshell, I want to be able to do this:1) int* t= new int;  //or any user defined class for that matter2) int* i = new int[3]; //ditto3) delete t;4) delete[] i;To be overloaded to malloc and free formats. I have managed to overload the first and third forms, but I dunno how to the same for arrays using malloc and free. Help!   
static void* operator new[](size_t);static void operator delete[](void*);;  Those things already use malloc and free internally, what could you possibly be trying to achieve?   ;  Er.. I suppose I better explain my situation a bit more fully! :DDue to various reasons (ARM compiler being the primary one), I do not have access to the standard new and delete operators (they are not allowed). However, malloc and free are available. So, I was thinking of overloading the new and delete operators so that my source will remain unchanged but via overloading will actually use malloc and free.The problem is that the non-array forms of new and delete work all right but if my source uses the new[] and delete[] forms the ARM compiler refuses to compile. We believe that the problem is due to the fact that the malloc requires a sizeof * number_of_data to do the job, which our overloading isn't doing.As of now, our overloads for new and delete looks like this:static void* operator new(igUInt32 size){ void* Ptr; Ptr = MALLOC(size); MEMSET(Ptr, 0, size);return Ptr;}static void operator delete(void* Ptr){if(Ptr){ FREE(Ptr);Ptr = NULL;}}NOTE: MALLOC/FREE = malloc/free in BREW since std libraries aren't included.My qustion then boils down to whether I can use the following forms with the above code, and if not how I can go about it?1) user_data* ud = new user_data;2) user_data* ud2 = new user_data[3]; //compiler refuses this3) delete ud;4) delete []ud2;EDIT: Apologies for the multiple posts earlier. Browser problem is all I can say. :O   ;  Be careful with the memset(..,0,..) you use in your overridden new-operator. Default new does not clear the memory, so if you start depending on your own new doing that for you, you'll have some problems when you want to compile your code with the default new.   ;  Thanks for explaining you predicament.Your new and delete could be better written as simplystatic void* operator new(igUInt32 size){ void* Ptr = MALLOC(size);#ifdef DEBUGMEMSET(Ptr, 0xCC, size);#endifreturn Ptr;}static void operator delete(void* Ptr){FREE(Ptr);}As mentioned, the memset to zeros isn't normal and you shouldn't expect it, rather you'd want to mimic the normal behaviour as closely as possible.'free' also handles NULL pointers already, and there was no point in writing Ptr = NULL either as it has no effect on the pointer you called delete on. (Unless your FREE really isn't the same as free)You may also want to make 'new' throw a bad alloc exception if malloc returns NULL.There's a bit more you could do too I expect. However I'm not sure how to help out with the [] versions.   ;  Do you mind my asking why you'd want to overload new and delete when they do the job just fine?The only reason I can imagine you'd want to modify the new and delete would be to output debug information or perhaps garbage collection?   ;  Leo, for the reasons see my post 3 posts above yours. :)iMalc, thanks for the pointers (pun intended) to memset and null checks. Actually, we aren't sure what the internal FREE and MALLOC workings are, so for safety reasons we implemented those extra checks. So far things have worked out okay. :)Bono, when we do have the default new available we revert to using the normal new operators and not the malloc versions above (through some ugly #defines), so the memset version is *only* for the FREE and MALLOC versions we are using above. I do believe both of you are right thought and that the extra bits of checking are redundant. I'll have a go at removing them and seeing if they work fine. :)   ;  I sure hope all of these implementations are static class members, since it's illegal for an allocation or deallocation function to be declared static at global scope.Enigma   ;  Sorry to dredge this out of obscurity but I thought I'll have a last shot at this. :(Apparently, I need to overload the new[] and delete[] at global scope since BREW doesn't do that. Now then, are therey *any* examples of how they can be overloaded at global scope using malloc and free?Enigma, interesting comment regarding new and delete as static functions in global scope, because this is exactly what we are doing at the moment. It works (the new and delete overloads using malloc and free) in BREW, so I'm not sure why you say they are illegal.   