Reputation: 361472
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.
Upvotes: 5
Views: 4020
Reputation: 92864
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>
)
Upvotes: 6
Reputation: 5693
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();
Upvotes: 15
Reputation: 6055
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.
Upvotes: 1
Reputation: 163248
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");
}
Upvotes: 5
Reputation: 72479
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
.
Upvotes: 5
Reputation: 231163
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.
Upvotes: 12
Reputation: 5338
Upvotes: 1
Reputation: 77752
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.)
Upvotes: 0
Reputation: 1105
new can certainly fail.
Constructors are allowed to throw exceptions. You can also run out of memory when creating a new object.
Upvotes: 1