Thread: Checking if memory was successfuly allocated.

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    97

    Checking if memory was successfuly allocated.

    Someone told me that doing the following to check if the allocation of memory was successful is wrong.
    Code:
    int *tempInteger = new int[10];
    if(!tempInteger)
       // ERROR.
    Is that wrong or is it the correct way to check if the allocation was successful?
    If not, what is the proper way to do that?

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    FAQ > Explanations of... > Dynamic Memory Allocation: new and delete (C++)
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    What the FAQ doesn't say is that it's really compiler/library dependant.

    Older standard C++ libraries will return 0 if new fails. Newew libraries will throw std::bad_alloc unless you install your own handler via std::set_new_handler().

    So check your documentation to see how new really behaves.

    gg

  4. #4
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    According to the C++ standard if memory allocation fails, bad_alloc exception is thrown, otherwise a non-null pointer is returned.
    This is beacuse new operator has prototype similiar to this:
    Code:
    void * operator new (size_t ) throw (bad_alloc);
    The header <new> defines several functions that manage dynamic allocation.

    You should do allocation checking wizh something like this:
    Code:
    #include <new>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        try
        {
            int* test = new int[10000];
        }
        catch(bad_alloc& bad)
        {
             cout<< bad.what()<<endl;
        }
    }
    If you wish not to throw exception in case of allocation failed, then use nothrow. For example:
    Code:
    ...
    int* test = new (nothrow) int[100000];
    
    if (!test)
    {
        cout<<"Allocation failed!"<<endl;
    }
    ...
    I encourage you to use exceptions.

    I hope this helps!

    - Micko
    Last edited by Micko; 04-12-2005 at 03:18 PM.
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    What the standard says and what an implementation does aren't always the same - so one should always RTFM. For example, I work with embedded compilers that don't support C++ exceptions - but I do use STLPort on those platforms.

    But to further clarify what the standard does say....
    >> ...if memory allocation fails, bad_alloc exception is thrown, otherwise a non-null pointer is returned
    In a standards conforming implementation, new could behave differently by installing your own new handler via std::set_new_handler() - which when called can:
    - make more storage available for allocation and return (ie. delete something)
    - throw bad_alloc or a class derived from bad_alloc
    - call abort() or exit()

    So if your implementation is standards comforming - then your friend is correct in that it is wrong.

    gg
    Last edited by Codeplug; 04-12-2005 at 04:32 PM.

  6. #6
    Registered User
    Join Date
    Oct 2003
    Posts
    97
    Cool, thanks for the answers, now I can handle those problems properly.

  7. #7
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    In MSVC.NET, new doesn't throw if <new> has not been included (either directly or indirectly).
    Code:
    #include <iostream>
    using namespace std;
    
    int main(void)
    {
    	try
    	{
    		char* p = new char[0xffffffff - 1];
    	}
    	catch (bad_alloc& bad)
    	{
    		cout << "Whoops" << endl;
    	}
    
    	cin.get();
    }
    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	char* p = new char[0xffffffff - 1];
    
    	if (!p) printf("Whoops\n");
    
    	getchar();
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutex and Shared Memory Segment Questions.
    By MadDog in forum Linux Programming
    Replies: 14
    Last Post: 06-20-2010, 04:04 AM
  2. Checking for memory leaks
    By Bladactania in forum C Programming
    Replies: 5
    Last Post: 02-10-2009, 12:58 PM
  3. Check if there is memory allocated
    By The Wazaa in forum C++ Programming
    Replies: 3
    Last Post: 04-23-2006, 05:48 AM
  4. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM
  5. Memory handler
    By Dr. Bebop in forum C Programming
    Replies: 7
    Last Post: 09-15-2002, 04:14 PM