Standards

Okay, I know that you are to delete arrays with [] and non-arrays without it.
I am wondering however what is considered an array, and what is not.

How would you delete these?

char *MyVar = new char[0];
char *MyVar = new char[1];
LVL 8
List244Asked:
Who is Participating?
 
AxterConnect With a Mentor Commented:
Hi List244,
If you use new[], than you need to use delete[].
If you just use new, than you need to use delete.

With both your examples, you would need to use delete[]

David Maisonave (Axter)
Cheers!
0
 
jkrCommented:
These are arrays (technically). You definitively will need to use

delete [] MyVar;

for everything you allocated using 'new <type>[]'. An argument of '1' is valid, '0' isn't. The standard states that "every constant-expressionin a direct-new-declarator shall [...] evaluate to a strictly positive value"
0
 
jkrCommented:
Ooops, forgot the source for the statement: '5.3.4 New, Paragraph 6"
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
List244Author Commented:
So, if 0 is invalid, is its behavior undefined?
0
 
jkrCommented:
I'd not rely on the result. Yet the standard also only states that the result is undefined for negative values. Even if the return value is a valid pointer, what would you store in a memory region of 0 bytes?
0
 
jkrCommented:
Sorry, paragraph 7 explains it:

"When the value of an expression in a direct-new-declarator is zero, the allocation function is called to allocate an array with no elements"
0
 
AxterCommented:
>>So, if 0 is invalid, is its behavior undefined?

Zero is not invalid, and it's results are defined.
0
 
List244Author Commented:
Well, I will explain what I am doing.  I have a class, which has a string of no specific size.
This string is at some point going to be created a place in memory, and of course at some
point going to be destroyed in the destructor.  However, if in this class, it is not initialized with
a value, I need to make sure it exists anyway, in case of early destruction.  Would I then be
safe with new char[0]?  Or would I be better off with char[1]?

Since it is the standards that it allocates an array with no elements, I assume it would be safe as
0, but what about compilers such as VC++ which do not have all the current standards.  Will these
cause problems?
0
 
jkrConnect With a Mentor Commented:
If the string member isn't present or has no elements, set the pointer to NULL. That's IMO the safest way. BTW, even VC++ conforms to that, tried it with VC6/7 and g++.
0
 
AxterCommented:
>>Would I then be safe with new char[0]?  Or would I be better off with char[1]?

If you have not characters, than you should leave the value as a NULL value.

You can safely call detete[] on a NULL value.
0
 
AxterCommented:
>>what about compilers such as VC++ which do not have all the current standards

Even older compilers allow for calling delete[] on a NULL value, so if you're really worried about behavior on older compilers, I would recommend setting the value to NULL instead of new char[0].

Not only is this method safer, but it's also more efficient.
0
 
List244Author Commented:
Alright, thank you both for the help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.