List244
asked on
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];
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];
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Ooops, forgot the source for the statement: '5.3.4 New, Paragraph 6"
ASKER
So, if 0 is invalid, is its behavior undefined?
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?
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"
"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"
>>So, if 0 is invalid, is its behavior undefined?
Zero is not invalid, and it's results are defined.
Zero is not invalid, and it's results are defined.
ASKER
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?
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?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>>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.
If you have not characters, than you should leave the value as a NULL value.
You can safely call detete[] on a NULL value.
>>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.
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.
ASKER
Alright, thank you both for the help.
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"