8

I saw some code written by another developer that looks something like this:

var stringBuilder = new StringBuilder();

if(stringBuilder == null)
{
    // Log memory allocation error
    // ...
    return;
}

(It is ALL over the place in the code )

Question 1: Would that error logging code even get called? If there was no memory, wouldn't an System.OutOfMemoryException be thrown on that first line?

Question 2: Can a call to a constructor ever return null?

John B
  • 20,062
  • 35
  • 120
  • 170
  • 2
    Technically it's possible for proxy classes and nullables, as Marc Gravell has pointed out, but it's such a pathological case that it's not worth considering: http://stackoverflow.com/questions/194484/whats-the-strangest-corner-case-youve-seen-in-c-or-net – Dan Bryant Jul 19 '10 at 19:50

5 Answers5

16

You're correct, and that code is wrong. It will throw OutOfMemoryException on a failure. This is clear in the documentation:

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

Constructors don't return anything, let alone null. They manipulate an object that's already been allocated.

Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
2

My assumption is that the coder used to work in C++, and doesn't know how things work in C#.

James Curran
  • 101,701
  • 37
  • 181
  • 258
  • 4
    The code is equally wrong for C++. C++ `new` throws `bad_alloc` when allocation fails, unless you explicitly tell it not to. – Matthew Flaschen Jul 19 '10 at 18:45
  • That is exactly what one of the interns said here. I myself haven't touched much C++, and not in a LONG time. Thanks. – John B Jul 19 '10 at 18:53
  • @Matthew: yes -- if you are using Standard C++ (and know what you are doing). Pre-Standard C++ returned null, and clearly the author isn't keeping his skills current. – James Curran Jul 19 '10 at 19:54
2

Now, this code is a different story:

StringBuilder stringBuilder = null;

try { stringBuilder = new StringBuilder(); } catch(Exception) {}

if(stringBuilder == null)
{
    // Log memory allocation error
    // ...
    return;
}

In that case, string builder could (conceivably) be null.

Brian Genisio
  • 47,787
  • 16
  • 124
  • 167
  • 1
    Actually, the StringBuilder wouldn't be `null`, it would be uninitialized. – Toby Jul 19 '10 at 18:53
  • 2
    Actually, it will not even compile. But I think we can all assume Brian meant to write `StringBuilder stringBuilder = null;` instead :) – Brian Gideon Jul 19 '10 at 18:58
  • 2
    Sorry guys. Fixed the code in the example. Who needs a compiler? I should just write a service that sends my code off to SO to be evaluated :) – Brian Genisio Jul 19 '10 at 19:04
1
  1. No. An OutOfMemoryException will be thrown if there isn't enough memory available to allocate an object.
  2. No
thecoop
  • 45,220
  • 19
  • 132
  • 189
0

Here is a better version of the code. You would have much bigger problems if there is not enough memory to allocate a reference though.

StringBuilder stringBuilder = null;

try {
   stringBuilder = new StringBuilder();
}
catch(OutOfMemoryException) {
   // log memory error
}
Dmitry S.
  • 8,373
  • 2
  • 39
  • 49