Page 1 of 1

new and delete

Posted: Fri Dec 20, 2002 1:45 pm
by pskyboy
Hey Guys

Has anyone managed to write there own new and delete functions yet so you can do dynamic allocation in c++. If so i would love to have a look at your source as this is the next thing i need to do.

Peter

Re:new and delete

Posted: Fri Dec 20, 2002 7:31 pm
by Tim

Code: Select all

void *operator new(size_t size)
{
    return malloc(size);
}

operator delete(void *ap)
{
    free(ap);
}
Implementing malloc and free is left as an exercise to the reader...

Re:new and delete

Posted: Sat Dec 21, 2002 4:14 am
by pskyboy
Thanks for that

Is there a standard libary that this needs to go in, ie what is the name of the file i need to stick it in for my compiler to find it. Or do i just include it like any other function in a seperate file.

Peter

Re:new and delete

Posted: Sat Dec 21, 2002 4:38 am
by Tim
Put it anywhere you like. It's probably a good idea to put it in your C library, so that all applications can use new and delete.