How does this work?

D

doublemaster007

new char[0] //
char str[0];

what will happen? will the memory allocated? how to delete?
 
D

doublemaster007

new char[0]  //
char str[0];
what will happen? will the memory allocated? how to delete?

Dynamically allocating an array of size 0 is OK, the system will
allocate the memory (of size 0, for all you care, since you ask it to do
that), but the requirement of the language is that the pointer you get
is otherwise perfectly valid, and requires to be delete[]'ed.

Declaring an array of 0 elements was not legal last time I looked.

V

Zero bytes will be allocated?? if do
new char[0] many times,we will be getting the pointer which points to
same location?
 
T

tommy.hinks

new char[0]  //
char str[0];
what will happen? will the memory allocated? how to delete?
Dynamically allocating an array of size 0 is OK, the system will
allocate the memory (of size 0, for all you care, since you ask it to do
that), but the requirement of the language is that the pointer you get
is otherwise perfectly valid, and requires to be delete[]'ed.
Declaring an array of 0 elements was not legal last time I looked.

Zero bytes will be allocated?? if do
new char[0] many times,we will be getting the pointer which points to
same location?

MS Visual Studio will give an error for declaring an array of zero
size, which "suggests" that this is illegal.

T
 
B

Bo Persson

new char[0] //
char str[0];
what will happen? will the memory allocated? how to delete?

Dynamically allocating an array of size 0 is OK, the system will
allocate the memory (of size 0, for all you care, since you ask it
to do that), but the requirement of the language is that the
pointer you get is otherwise perfectly valid, and requires to be
delete[]'ed.

Declaring an array of 0 elements was not legal last time I looked.

V

Zero bytes will be allocated?? if do
new char[0] many times,we will be getting the pointer which points
to same location?

No, the returned pointers must be different (until you delete[] some
of them, and they are recycled).

One reason for allowing zero size is that the parameter can be a
variable

char* p = new char;

will work, even if i == 0.


Bo Persson
 
P

pasa

On Feb 10, 4:43 pm, "(e-mail address removed)" > > > new char[0]  //
Zero bytes will be allocated?? if do
new char[0] many times,we will be getting the pointer which points to
same location?- Hide quoted text -

You can't legally tell what happens in the background. The specified
behavior is that you get back a non-NULL pointer, that you can safely
pass around, and shall finally pass to delete[] once, to avoid a
memory leak.
You must not dereference that pointer.

In practice the common behavior is likely one of:
- you get a special pointer value for all such allocations
- you get a 1 or few bytes long new memory block allocated
- you get 1 object's worth of allocation

the standard says: (5.3.4p7)
"When the value of the expression in a direct-new-declarator is zero,
the allocation function is called to allocate an array with no
elements. The pointer returned by the newexpression is non-null.
[Note: If the library allocation function is called, the pointer
returned is distinct from the pointer to any other object. ] "
 
T

tommy.hinks

(e-mail address removed) wrote:
new char[0]  //
char str[0];
what will happen? will the memory allocated? how to delete?
Dynamically allocating an array of size 0 is OK, the system will
allocate the memory (of size 0, for all you care, since you ask it to do
that), but the requirement of the language is that the pointer you get
is otherwise perfectly valid, and requires to be delete[]'ed.
Declaring an array of 0 elements was not legal last time I looked.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Zero bytes will be allocated?? if do
new char[0] many times,we will be getting the pointer which points to
same location?
MS Visual Studio will give an error for declaring an array of zero
size, which "suggests" that this is illegal.

Allocating zero bytes and declaring a zero-size array are two different
things, Tommy.

int main(int argc, char **argv)
{
     char zerosizedarray[0]; // error - not allowed

     char *p = new char[argc - 1];   // perfectly fine,
                                     // even if 'argc' is 1

     delete[] p;

}

V

Sure Victor, I agree that allocation and declaration are different
things. Perhaps I wasn't clear enough when I wrote "declaring an array
of zero size". Typically, declaring an array of a certain size refers
to the size being a compile-time constant. However, thanks for
pointing this out in the case that there were misunderstandings.

T
 
J

James Kanze

new char[0] //
char str[0];
what will happen? will the memory allocated? how to delete?
Dynamically allocating an array of size 0 is OK, the system
will allocate the memory (of size 0, for all you care, since
you ask it to do that), but the requirement of the language is
that the pointer you get is otherwise perfectly valid, and
requires to be delete[]'ed.

Sort of. The requirement is that the pointer returned will
compare unequal to any other pointer returned from a new
operator, and unequal to the address of any actual object. And
won't be null. And the implementation is allowed the throw
bad_alloc; the allocation of 0 bytes can fail.

In practice, this means that the system will actually allocate
something.
Declaring an array of 0 elements was not legal last time I
looked.

It's not, and it's a commonly used trick to implement
constraints in templates:
char something_I_want_in_the_error_message
[ evaluates_true_or_false ] ;
true converts to 1, and the line is legal, false converts to 0,
and you get an error, hopefully with the name of the variable in
the error message.
 
J

James Kanze

On Feb 10, 4:43 pm, "(e-mail address removed)" > > > new char[0] //
char str[0];
Zero bytes will be allocated?? if do new char[0] many
times,we will be getting the pointer which points to same
location?- Hide quoted text -
You can't legally tell what happens in the background. The
specified behavior is that you get back a non-NULL pointer,
that you can safely pass around, and shall finally pass to
delete[] once, to avoid a memory leak.
You must not dereference that pointer.
In practice the common behavior is likely one of:
- you get a special pointer value for all such allocations
- you get a 1 or few bytes long new memory block allocated
- you get 1 object's worth of allocation

The first is illegal, and the other two are examples of a poor
implementation. All of the allocation algorithms I know entail
some overhead in the block, in order to find the size in the
delete; array allocations may entail even more, in order to find
the number of elements (necessary if the elements have
non-trivial destructors). A good implemenation won't allocate
any more than this overhead---effectively allocating 0 bytes.
 
P

pasa

Declaring an array of 0 elements was not legal last time I
looked.

It's not, and it's a commonly used trick to implement
constraints in templates:
    char something_I_want_in_the_error_message
[ evaluates_true_or_false ] ;
true converts to 1, and the line is legal, false converts to 0,
and you get an error, hopefully with the name of the variable in
the error message.

Yet gcc accepts it (at least to 3.4.6) as some "extension", you have
to use negative extent to force an error.
 
J

James Kanze

Declaring an array of 0 elements was not legal last time I
looked.
It's not, and it's a commonly used trick to implement
constraints in templates:
char something_I_want_in_the_error_message
[ evaluates_true_or_false ] ;
true converts to 1, and the line is legal, false converts to 0,
and you get an error, hopefully with the name of the variable in
the error message.
Yet gcc accepts it (at least to 3.4.6) as some "extension",
you have to use negative extent to force an error.

Hmmm. I get:
array0.cc:10: error: ISO C++ forbids zero-size array `dummy'
both with 3.4.0 and 4.1.0. Are you sure you're invoking it
correctly? (You need at least -std=c++98 -pedantic, and
probably some additional options, to get a C++ compiler.)
 
P

pasa

Hmmm.  I get:
    array0.cc:10: error: ISO C++ forbids zero-size array `dummy'
both with 3.4.0 and 4.1.0.  Are you sure you're invoking it
correctly?  (You need at least -std=c++98 -pedantic, and
probably some additional options, to get a C++ compiler.)

I no longer have access to that build, only the make/config files,
don't see those options... so probably that is tha cause. I recall I
had to modify the static assert-creating macro to use 2*x-1 instead of
x, so it actually worked.
(the codebase uses some extensions here and there, so not playing the
stricter options may be intentional, but more likely just left that
way to avoid work...)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,901
Members
47,439
Latest member
elif2sghost

Latest Threads

Top