D
doublemaster007
new char[0] //
char str[0];
what will happen? will the memory allocated? how to delete?
char str[0];
what will happen? will the memory allocated? how to delete?
[email protected] said: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
[email protected] said: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?
[email protected] said: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?
Zero bytes will be allocated?? if dochar str[0];
new char[0] many times,we will be getting the pointer which points to
same location?- Hide quoted text -
[email protected] said:(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
[email protected] said: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.
On Feb 10, 4:43 pm, "(e-mail address removed)" > > > new char[0] //Zero bytes will be allocated?? if do new char[0] manychar str[0];
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
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.
It's not, and it's a commonly used trick to implementDeclaring an array of 0 elements was not legal last time I
looked.
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.)
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.