Using new and new (nothrow)...

SephirothSephiroth Fayetteville, NC, USA
Alright, I know that if I use "p = new (nothrow) whatever;" and new fails to allocate an instance of the specified class or variable, I can check my pointer to see if it is equal to NULL. However, if I use new without the nothrow option and instead use the catch method to catch exceptions, I am running into an interesting predicament. Let's assume I have a class with an integer pointer for simplicity's sake. I call a method that attempts to create an instance of the pointer using new without nothrow. Whether it allocates or I catch a failure is regardless. When the class destructor is called, how would I tell if the pointer was allocated? If it failed and I caught the failure but only logged it, would the pointer still be NULL, or would it be something else? What if I called delete prior to the class being destroyed? How do I tell if a pointer is allocated after a new has been used on it?

Just for reference, if my code calls delete on any pointer, it immediatly sets the pointer to NULL so the other parts of my code can check for NULL prior to calling new on it. The thing is, I may not always know, so I need to know if when new fails, it sets the pointer to NULL or something else.

*EDIT*

I thought I would post my dilemna in actual code for clarification.
[code]
//This method would ensure that the pointer is NULL, but is it needed?
try
this->pWindow = new VGLWindow;
catch(std::bad_alloc &baError)
{
this->pWindow = NULL;
return false;
}

//This method may be what I need if the new failure returns NULL
try
this->pWindow = new VGLWindow;
catch(std::bad_alloc &baError)
return false;
[/code]
If the operator new returns NULL upon throwing an exception, I won't have to assign NULL every time it fails, assuming it does. This would be a minor speed increase and less code overall.

-[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]

Comments

  • iirc, you dont need to assign the ptr to NULL if new throws bad_alloc. The ptr will retain its previous value.

    So, if it was already initialized to NULL, the ptr would still be NULL if new throws bad_alloc, so the assignment is not needed...

    [code]
    try
    this->pWindow = new VGLWindow;
    catch(std::bad_alloc &baError)
    {
    // this->pWindow = NULL; // Not needed here, assuming it was initialized to NULL
    return false;
    }
    [/code]

    I personally dont think it would be a noticeable increase at all, tbh. It is too small of an operation to be noticeable (one or two MOV instructions, depending on register usage), even in a loop.

    [hr][size=1][leftbr].:EvolutionEngine[rightbr][leftbr].:MicroOS Operating System[rightbr][leftbr][link=http://www.brokenthorn.com]Website :: OS Development Series[rightbr][/link][/size]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion