[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Standards

Posted on 2006-05-10
12
Medium Priority
?
297 Views
Last Modified: 2010-04-01
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];
0
Comment
Question by:List244
  • 5
  • 4
  • 3
12 Comments
 
LVL 30

Accepted Solution

by:
Axter earned 1000 total points
ID: 16651020
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
 
LVL 86

Expert Comment

by:jkr
ID: 16651093
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
 
LVL 86

Expert Comment

by:jkr
ID: 16651101
Ooops, forgot the source for the statement: '5.3.4 New, Paragraph 6"
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 8

Author Comment

by:List244
ID: 16651121
So, if 0 is invalid, is its behavior undefined?
0
 
LVL 86

Expert Comment

by:jkr
ID: 16651176
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
 
LVL 86

Expert Comment

by:jkr
ID: 16651194
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
 
LVL 30

Expert Comment

by:Axter
ID: 16651235
>>So, if 0 is invalid, is its behavior undefined?

Zero is not invalid, and it's results are defined.
0
 
LVL 8

Author Comment

by:List244
ID: 16651244
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
 
LVL 86

Assisted Solution

by:jkr
jkr earned 1000 total points
ID: 16651278
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
 
LVL 30

Expert Comment

by:Axter
ID: 16651289
>>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
 
LVL 30

Expert Comment

by:Axter
ID: 16651310
>>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
 
LVL 8

Author Comment

by:List244
ID: 16651320
Alright, thank you both for the help.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question