is that code is valid?

V

Virtual_X

void st(const char *a)
{
cout << a;
delete(a);
}

int main ()
{
char foo[]= "CPP";

st(foo);
cout << "\n\n" << foo;

system("PAUSE");
return 0;
}

particular in the function st when i use delete(a)

is that also delete foo not only a
 
S

Scott McPhillips [MVP]

Virtual_X said:
void st(const char *a)
{
cout << a;
delete(a);
}

int main ()
{
char foo[]= "CPP";

st(foo);
cout << "\n\n" << foo;

system("PAUSE");
return 0;
}

particular in the function st when i use delete(a)

is that also delete foo not only a

The code is not valid. Use delete only for memory allocated with new.
Deleting something else will lead to a crash.

If there's something you don't understand try asking a question about it
instead of showing some crazy mixed up code.
 
R

Rolf Magnus

Scott McPhillips said:
Virtual_X said:
void st(const char *a)
{
cout << a;
delete(a);
}

int main ()
{
char foo[]= "CPP";

st(foo);
cout << "\n\n" << foo;

system("PAUSE");
return 0;
}

particular in the function st when i use delete(a)

is that also delete foo not only a

The code is not valid.

To be more precise: It has undefined behavior.
 
D

Default User

Virtual_X said:
void st(const char *a)
{
cout << a;
delete(a);
}

int main ()
{
char foo[]= "CPP";

st(foo);
cout << "\n\n" << foo;

system("PAUSE");
return 0;
}

particular in the function st when i use delete(a)

is that also delete foo not only a

Yesterday, Jim Langston told you:

"delete should only be used on memory allocated with new, use delete[]
with new[]"


If you aren't going to LISTEN to what people you, then stop wasting
everyone's time.



Brian
 
V

Virtual_X

Yesterday, Jim Langston told you:
"delete should only be used on memory allocated with new, use delete[]
with new[]"

If you aren't going to LISTEN to what people you, then stop wasting
everyone's time.

Brian

i read in an ebook in the internet that you can delete pointers
which not intialise with new[]
 
D

Default User

Virtual_X said:
Yesterday, Jim Langston told you:

"delete should only be used on memory allocated with new, use
delete[] with new[]"

If you aren't going to LISTEN to what people you, then stop wasting
everyone's time.

Brian

i read in an ebook in the internet that you can delete pointers
which not intialise with new[]

Which book? I'd say the chances are better that you misinterpreted what
you read rather than a book, even an ebook, getting something that
fundamentally wrong.




Brian
 
K

Kai-Uwe Bux

Virtual_X said:
Yesterday, Jim Langston told you:

"delete should only be used on memory allocated with new, use delete[]
with new[]"

If you aren't going to LISTEN to what people you, then stop wasting
everyone's time.

Brian

i read in an ebook in the internet

Which one? If you did not misunderstand what is written there, that book is
dangerously wrong.
that you can delete pointers which not intialise with new[]

It is undefined behavior to delete() any pointer not obtained by new()
unless the pointer is 0.

It is undefined behavior to delete[] any pointer not obtained by new[]
unless the pointer is 0.

It is undefined behavior to call delete (either version) on any dangling
pointer.


For details on delete() and delete[], see the standard, section 5.3.5.
Several other fun ways of getting undefined behavior using delete are
mentioned there.


Best

Kai-Uwe Bux
 
B

BobR

Default User said:
Virtual_X said:
Yesterday, Jim Langston told you:
"delete should only be used on memory allocated with new, use
delete[] with new[]"

If you aren't going to LISTEN to what people you, then stop wasting
everyone's time.
Brian

i read in an ebook in the internet that you can delete pointers
which not intialise with new[]

Which book? I'd say the chances are better that you misinterpreted what
you read rather than a book, even an ebook, getting something that
fundamentally wrong.

{
int *x(0); // "not intialise with new[]"
delete x;
}
 
D

Default User

BobR said:
Default User said:
Virtual_X said:
Yesterday, Jim Langston told you:
"delete should only be used on memory allocated with new, use
delete[] with new[]"

If you aren't going to LISTEN to what people you, then stop
wasting everyone's time.

{
int *x(0); // "not intialise with new[]"
delete x;
}

Yes, that is correct.



Biran
 
J

James Kanze

[Just a nit concerning the language...]
It is undefined behavior to delete() any pointer not obtained by new()
unless the pointer is 0.
It is undefined behavior to delete[] any pointer not obtained by new[]
unless the pointer is 0.

A pointer cannot be 0. 0 is an integer value, and pointers
aren't ints. The correct term is "unless the pointer is null".
(0 will convert to a null pointer in certain, specific contexts,
but it is precisely that---the integral constant 0 is
*converted* to something else---a null pointer.)

Of course, you could also say "unless the pointer compares equal
to 0":).
 
K

Kai-Uwe Bux

James said:
[Just a nit concerning the language...]
It is undefined behavior to delete() any pointer not obtained by new()
unless the pointer is 0.
It is undefined behavior to delete[] any pointer not obtained by new[]
unless the pointer is 0.

A pointer cannot be 0. 0 is an integer value, and pointers
aren't ints. The correct term is "unless the pointer is null".

I take it that doubles or unsigned ints cannot be 0 either. Man, that sucks.

I am not implying that the arithmetic conversions and promotions are the
same as the special conventions regarding 0. However, your point about a
type mismatch seems to apply in either case.

Generally, I am not so sure whether I like a language convention that
says "x == 0" may not be read as "x is 0" and "x = 0" as something
like "set x to 0". Your point regarding correctness is well-taken. However,
I don't find the _correct_ readings more useful or easier to comprehend
than the incorrect ones.


Best

Kai-Uwe Bux
 
R

Rolf Magnus

Kai-Uwe Bux wrote:

I take it that doubles or unsigned ints cannot be 0 either. Man, that
sucks.

Also, there is no way in C++ to write that value as a decimal literal. 0 is
octal.
 
J

James Kanze

James said:
[Just a nit concerning the language...]
It is undefined behavior to delete() any pointer not obtained by new()
unless the pointer is 0.
It is undefined behavior to delete[] any pointer not obtained by new[]
unless the pointer is 0.
A pointer cannot be 0. 0 is an integer value, and pointers
aren't ints. The correct term is "unless the pointer is null".
I take it that doubles or unsigned ints cannot be 0 either. Man, that sucks.

It depends. If you are speaking about the literal constant
written 0, it has type int. If you are speaking about the
numeric value 0 (which is what I would assume in running English
text), then there's no problem. Double and float do represent
numeric values.

Pointers don't. That's really my point.
I am not implying that the arithmetic conversions and promotions are the
same as the special conventions regarding 0. However, your point about a
type mismatch seems to apply in either case.

I think I mis-expressed my point. I should have said "0 is a
numeric value, and pointers don't have numeric values."
Generally, I am not so sure whether I like a language
convention that says "x == 0" may not be read as "x is 0" and
"x = 0" as something like "set x to 0". Your point regarding
correctness is well-taken. However, I don't find the _correct_
readings more useful or easier to comprehend than the
incorrect ones.

As I say, it's a nit.
 

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

Forum statistics

Threads
474,291
Messages
2,571,453
Members
48,137
Latest member
IndiraMcCo

Latest Threads

Top