Skip to main content
GameDev.net gamedev.net
🔒 Locked

delete

Started by yummy_potatoes Mar 14, 2003 at 11:42 PM 3 replies 200+ views
Original Post
yummy_potatoes
yummy_potatoes
I feel stupid for even asking this, but I skimmed through my c++ book, and it doesn't seem to have an example of this: My Question:
    
int *a, *b;
a = new int[50];
b = new int[50];

/////////////////////////////////////////

// Are both of these methods of deletion the same?

/////////////////////////////////////////


delete [] a;
delete [] b;

//or this method

delete [] a, b;
  
[edited by - yummy_potatoes on March 14, 2003 12:43:58 AM]
"I once thought I was wrong, but then I was mistaken." - A4
Beer Hunter
Beer Hunter
Short answer: No.

Long answer: The comma is used for two purposes: separating parameters, and as a binary operator. As a binary operator, it returns the right-hand value. When you write delete[] a, b;, the comma is considered as a binary operator, returning b and doing nothing with a. Therefore, only b will be deleted.
SiCrane
SiCrane
quote:
Original post by Beer Hunter
When you write delete[] a, b;, the comma is considered as a binary operator, returning b and doing nothing with a. Therefore, only b will be deleted.


Almost right -- you forgot operator precedence. delete [] a, b will be parsed as (delete [] a), b

So only a will be delete []''d and the expression will return b.
Beer Hunter
Beer Hunter
Quite right. Sorry for any confusion I caused.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.