5

In C++, we usually see and write code like,

Sample sample=new Sample();
if ( sample == NULL )
{
     std::cout<<"Memory allocation failed" << std::endl;
}

But in C#, I rarely see this: (at least I've never seen this)

Sample sample = new Sample();
if ( sample == null )
{
     Console.WriteLine("Memory allocation failed\n");
}

Means, in C#, we rarely check if a new failed or not. Why is it so? Does it have something to do with "In C#, new never fails"? Is there such a thing in C# that new never fails?

If it fails, then why such "check" is so rare in C#? I'm not talking about OutOfMemoryException, that is after all exception, not "check". I'm talking about the coding style.

2
  • 13
    "In C++, we usually see and write code like..." No we dont. A standards-conforming C++ implementation is required to indicate errors in new by throwing a std::bad_alloc exception. Some older C++ compilers returned a null pointer on error in new, but this is not the case anymore nowadays.
    – In silico
    Jan 22, 2011 at 7:19
  • 12
    A conforming new in C++ will never return a null pointer. If you "usually see and write code like" this, you're Doing It Wrong™. (You need the std::nothrow placement form of new to get a null pointer returned.)
    – Fred Nurk
    Jan 22, 2011 at 7:20

10 Answers 10

15

According to msdn

If the new operator fails to allocate memory, it throws the exception OutOfMemoryException.

http://msdn.microsoft.com/en-us/library/51y09td4%28v=vs.71%29.aspx

By the way, only old C++ compilers return 0 when they trying to allocate memory. Modern ones throwing std::bad_alloc exception. If you wish old behavior you need to write

Sample sample=new(std::nothrow) Sample();
2
12

In C#, new throws an OutOfMemoryException if it fails, so a NULL check is not necessary.

Incidentally, the same applies in C++ as well - new throws std::bad_alloc if it fails, so there really is no need to test against NULL in C++ either.

6

In C++, unlike malloc, new doesn't return NULL on failure. In fact, it throws a std::bad_alloc exception on failure.

If you want new to return NULL on failure, you have to explicitly tell it not to throw an exception. Use std::nothrow version for that purpose. (header file : <new>)

2
  • On certain non compliant compilers new can return NULL.
    – rve
    Jan 22, 2011 at 7:25
  • @rve : On conformant implementation new would never return NULL unless the std::nothrow version of new is used. Jan 22, 2011 at 7:28
5
Sample sample=new Sample();
if ( sample == NULL )
{
     // You will never get here!
     std::cout<<"Memory allocation failed" << std::endl;
}

We don't write this in C++ either. "Memory allocation failed" will never appear. In both languages you get an exception if the allocation fails. In C++ it's std::bad_alloc, in C# it's OutOfMemoryException.

5

If new fails, then there will be an OutOfMemoryException thrown.

So the equivalent of the C++ code you have would be this:

try { 
  Sample sample = new Sample();
} catch(OutOfMemoryException e) {
  Console.WriteLine("Memory allocation failed\n");
}
1

new can certainly fail.

Constructors are allowed to throw exceptions. You can also run out of memory when creating a new object.

1

There are an OutOfMemoryException in C#

1
  1. I rarely see it in C++
  2. It doesn't make sense in C#, because it simply will just throw an Exception
1

In C++ new can return NULL when using a non compliant compiler (see Will new return NULL in any case?)

But, the proper way in C++ is that new throws an exception when out of memory, just like C#.

The check for NULL is only needed on certain compilers.

0

Is this some test code you wrote? If so, I assume that you don't actually keep the reference to the object you created, meaning that the garbage collection will kick in and delete some of the objects you created previously.

That said, you can certainly run out of memory if you allocate enough memory and do keep references to it (at which point an exception will be thrown - a NULL pointer as a return value for memory allocations is more common in C, and in C++ versions written for video game consoles.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.