new and delete

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
pskyboy

new and delete

Post 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
Tim

Re:new and delete

Post 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...
pskyboy

Re:new and delete

Post 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
Tim

Re:new and delete

Post 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.
Post Reply