CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Join Date
    Apr 1999
    Location
    Dublin, Ireland
    Posts
    64

    allocating memory with new

    Hi,

    Is there any circumstance in which it is okay not to check for success after a "new" ? I am seeing a lot of code that attempts to create objects on the heap using new without there being a check.

    classx * myptr = new classx;
    if (myptr==NULL) // this is missing.......
    {
    }
    else // I succeeded
    {
    etc
    etc
    }



  2. #2
    Join Date
    May 1999
    Location
    Farnborough, Hants, England
    Posts
    710

    Re: allocating memory with new

    I guess most people assume that under normal circumstances, the 'new' would not fail - because of large memory in PCs and virtual memory, etc. If, however, you are using (allocating) a lot of memory, or creating a large number of large objects, then checking the return value is the good thing to do. There is no harm in it, it is good practice.

    Many people often don't check pointers obtained from functions, either - they assume that it will work because they expect everything to be running normally on their system - they expect their code to behave and produce the right results. That is one area, however, where I disagree - I check pointers form functions rigourously; there is a far higher risk of one of these being NULL than a 'new' failing.

    Any comments?



    --
    Jason Teagle
    [email protected]

  3. #3
    Join Date
    Apr 1999
    Posts
    396

    Re: allocating memory with new

    That is excellent practice. Ideally you should always check if new fails. But %99.999999 it works, so sometimes we programmers leave it out.


  4. #4
    Join Date
    Apr 1999
    Location
    Dublin, Ireland
    Posts
    64

    Re: allocating memory with new

    I too check the results of new and the value of pointers returned by functions.

    Thanks,

    Brendan


  5. #5
    Join Date
    Apr 1999
    Location
    Dublin, Ireland
    Posts
    64

    Re: allocating memory with new

    Thanks Todd,

    Brendan


  6. #6
    Join Date
    May 1999
    Posts
    8

    Re: allocating memory with new

    Hello,

    According to me the good programming style is that create a small template class which will simulate the idea of smart pointers.
    Use this template class to declare a pointer to any object and then allocate memory for this pointer.
    The constructor of this class will check for the success. If failed, will throw an exception.
    You use SEH in your program so that if smart pointer throws an exception your program can catch and process it.

    What I am trying to say is

    try
    {
    smart_ptr<any Object type> *Ptr = new (...)
    ....
    ....
    }
    catch(...)
    {
    exceptions are handled here.
    }

    At least this is the normal practice I use to follow in all my projects.

    Any comments?

    Thanks
    Chetan



    Nothing is impossible.

  7. #7
    Guest

    Re: allocating memory with new

    If you are using exceptions in C++, one is thrown in the event that new fails. So, instead of doing this sort of checking all over the place, you can catch the allocation failure.

    It makes the code much cleaner to read.


  8. #8
    Join Date
    May 1999
    Location
    Farnborough, Hants, England
    Posts
    710

    Re: allocating memory with new

    An interesting way of doing it, but doesn't it require more code to handle it? I.e. (I have never used template programming before (never found a need), so if the syntax is wrong, I apologise),

    ---

    BOOL bFailed = FALSE ;
    CMyObject *pMyObj ;

    try
    {
    pMyObj = smart_ptr<CMyObject>
    }
    catch(...)
    {
    bFailed = TRUE ;
    }
    end_catch

    if (!bFailed)
    {
    // Rest of code here...
    }



    ---

    versus:

    ---

    CMyObject *pMyObj ;

    pMyObj = new CMyObj ;
    if (pMyObj != NULL)
    {
    // Rest of code here...
    }



    ---

    ... or have I got the use of your smart pointer wrong?



    --
    Jason Teagle
    [email protected]

  9. #9
    Join Date
    May 1999
    Location
    Farnborough, Hants, England
    Posts
    710

    Re: allocating memory with new

    I'm not sure I agree; I feel

    ---

    CMyObject *pMyObj ;

    pMyObj = new CMyObject ;
    if (pMyObj != NULL)
    {
    }



    ---

    is neater and easier to follow than

    ---

    CMyObject *pMyObj ;

    try
    {
    pMyObj = new CMyObject ;
    }
    catch(...)
    {
    }
    end_catch



    ---

    This is especially true if you have to allocate several objects at the start of a program, where later objects rely on the pointers allocated earlier; trying to prevent later objects from being created if one of the dependent pointers fails with try-catch blocks becomes more awkward, since they encapsulate the failed condition (where you want things to stop) rather than the successful condition (where you want things to continue).

    I realise that you could issue a return command at the point of failure, but for the scenario above this would result in several returns within the same function - I was taught to have only one exit point in a routine, so this is a no-no for me.



    --
    Jason Teagle
    [email protected]

  10. #10
    Join Date
    May 1999
    Location
    Sydney, Australia
    Posts
    420

    Re: allocating memory with new


    I disagree with you...

    I find this harder to read:


    CMyObject *pMyObj;
    pMyObj = new CMyObject;
    if (pMyObj != NULL)
    {
    // report error
    }

    CMyObject *pMyObj2;
    pMyObj2 = new CMyObject;
    if (pMyObj2 != NULL)
    {
    // report error
    }

    CMyObject *pMyObj3;
    pMyObj3 = new CMyObject;
    if (pMyObj3 != NULL)
    {
    // report error
    }

    CMyObject *pMyObj4;
    pMyObj4 = new CMyObject;
    if (pMyObj4 != NULL)
    {
    // report error
    }

    CMyObject *pMyObj5;
    pMyObj5 = new CMyObject;
    if (pMyObj5 != NULL)
    {
    // report error
    }




    than this code:


    CMyObject *pMyObj;
    CMyObject *pMyObj2;
    CMyObject *pMyObj3;
    CMyObject *pMyObj4;
    CMyObject *pMyObj5;
    try
    {
    pMyObj = new CMyObject;
    pMyObj2 = new CMyObject;
    pMyObj3 = new CMyObject;
    pMyObj4 = new CMyObject;
    pMyObj5 = new CMyObject;
    }
    catch(...)
    {
    // report error
    }
    end_catch




    I find that using exceptions stops me from from having to return error codes through out the system
    When a problem occurs, an exception is raised and I can respond to it in a clean and object oriented way

    Sally




  11. #11
    Join Date
    May 1999
    Location
    Farnborough, Hants, England
    Posts
    710

    Re: allocating memory with new

    With the example you have given, I agree; the one catch for all errors is neater and more compact.

    However, as I have mentioned in another response, if some later objects require pointers allocated before, and you don't want them created if the dependencies fail, the try-catch becomes more unwieldy.

    Thanks for everyone's input.



    --
    Jason Teagle
    [email protected]

  12. #12
    Join Date
    Apr 1999
    Posts
    383

    Re: allocating memory with new

    The principle behind the try...catch is that you can write the body code of your function without worrying about failures, i.e. as if you assume everything will succeed, then you can catch any exceptions at the end. There is rarely a need for additional checks using a boolean 'failed' flag.

    I would code your example something like this:

    try {
    smart_ptr<CMyObject> spMyObj(new CMyObject());
    // Continue body of function here...
    }
    // End of function tidy up
    catch(smart_ptr_exception& spe)
    {
    // handle exeption
    }
    catch(...)
    {
    // exceptions from rest of code
    }

    [The smart_ptr syntax depends on the smart_ptr and what you want to do with it]

    In fact, in standard C++, it is the 'new' that throws the exception in this situation.

    If circumstances lead to more than one 'try' block in a function, I usually take it as an indication that it should be split up into further functions.

    Dave


  13. #13
    Join Date
    May 1999
    Location
    Sydney, Australia
    Posts
    420

    Re: allocating memory with new

    In that case I'd do this...


    CMyObject *pMyObj1 = NULL;
    CMyObject *pMyObj2 = NULL;
    CMyObject *pMyObj3 = NULL;
    CMyObject *pMyObj4 = NULL;
    CMyObject *pMyObj5 = NULL;
    try
    {
    pMyObj1 = new CMyObject;
    pMyObj2 = new CMyObject;
    pMyObj3 = new CMyObject;
    pMyObj4 = new CMyObject;
    pMyObj5 = new CMyObject;
    }
    catch(...)
    {
    // report error
    delete pMyObj1;
    delete pMyObj2;
    delete pMyObj3
    delete pMyObj4
    delete pMyObj5
    }
    end_catch






  14. #14
    Join Date
    May 1999
    Location
    Sydney, Australia
    Posts
    420

    Re: allocating memory with new

    I'd also put a ; last on the last 3 delete statements

    Sally


  15. #15
    Join Date
    Apr 1999
    Posts
    32

    Re: allocating memory with new

    I think that the throwing of an exception depends upon which new is called. CObject:: operator new thows an exception but I don't think template<class T> void *operator new does. E&OE, of course.

    --
    Daren Chandisingh

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured