-2

I was to delete [] some allocated memory if a condition is met: The condition is that double* m_x == nullptr

I am trying to do this:

(m_x == nullptr) ? (delete [] m_x);

However the compiler requires that I have the "else part" after a : like so:

(m_x == nullptr) ? (delete [] m_x) : ;

But I don't have anything to put after the colon.

Is there a way around this? Apart from:

if(m_x == nullptr)
        delete [] m_x;

Thanks,

EDIT

I meant if(m_x != nullptr) sorry copied it wrong

FreelanceConsultant
  • 13,167
  • 27
  • 115
  • 225

4 Answers4

11

You probably meant (m_x != nullptr) as the condition? In that case, you can simplify

(m_x != nullptr) ? (delete [] m_x);

to

delete[] m_x;

because the nullptr check is performed internally by delete and delete[], anyway.

If you really meant the condition (m_x == nullptr), then you can just get rid of the entire statement, because deleting a pointer only if it is null is always a no-op. Please explain why you think you want this.

fredoverflow
  • 256,549
  • 94
  • 388
  • 662
  • @ScarletAmaranth - except that it doesn't do what the question asked for, which was to `delete` **only if** the pointer is null. – Pete Becker Nov 18 '13 at 21:41
  • @PeteBecker Oh, I didn't realize that. I suppose this is a typo in the question? I cannot imagine why anybody would want to delete a null pointer? – fredoverflow Nov 18 '13 at 21:42
  • @FredOverflow - the code you wrote is probably right, but giving an answer that has different semantics really calls for an explicit statement that that's what's happening. – Pete Becker Nov 18 '13 at 21:44
9

Using an if statement is probably better anyway. Why is that not a good solution?

But if you insist on using the conditional operator, just use a dummy expression whose value can be ignored:

m_x == nullptr ? delete [] m_x : (void)0

(void)0 is the simplest way to create an expression of type void, needed here to match the type of delete [] m_x.

But if it's not part of a larger expression, I can think of no good reason not to use an if statement:

if (m_x == nullptr) delete [] m_x;

But since delete on a null pointer does nothing, it's difficult to understand why you'd want to do this in the first place.

If your actual intent is to delete the pointer if it's not null, then you'd want:

if (m_x != nullptr) delete [] m_x;

But again, since delete on a null pointer does nothing (thanks to @juanchopanza) for pointing that out in a comment), you might as well simplify it to:

delete [] m_x;
Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • 1
    There is no need for this though. `delete [] m_x;` would be way simpler. – juanchopanza Nov 18 '13 at 21:39
  • The idea of using zero doesn't work: Error is: error: second operand to the conditional operator is of type ‘void’, but the third operand is neither a throw-expression nor of type ‘void’ – FreelanceConsultant Nov 18 '13 at 21:53
  • @EdwardBird: I've updated my answer, changing `0` to `(void)0`. But *please* read the rest of the answer (and the other answers and comments) as well. You have your logic backwards, and even allowing for that, there's probably no good reason to use a conditional operator rather than an `if` statement. – Keith Thompson Nov 18 '13 at 22:01
7

The ternary operator is meant to be used in expressions, where you want to choose what value to return depending on a condition. It is a an if statement in the style of a functional language.

It is not meant to be used when you purely have side effects (such as freeing memory). Using an if statement is the right thing to do in that case.

Anders Abel
  • 67,989
  • 17
  • 150
  • 217
1

I don't think you are using that properly... Here is an example of how that statement is used...

int inValue = 5;
int i = 0;
int b = (inValue > 0) ? (i + 1) : (i);

It either assigns X or Y Result=(condition)?X:Y

IntStarFoo
  • 765
  • 8
  • 14