Can you delete a pointer without newing it up?

A

asimorio

Hi all,
If I don't new up a pointer, can I delete it?
see code below:

void A::foo()
{
char* buffer;
delete buffer;
return;
}


regards,
Vynce
 
R

Rolf Magnus

asimorio said:
Hi all,
If I don't new up a pointer, can I delete it?

Well, you can, but the behavior will be undefined unless the pointer was a
null pointer. In that case, nothing is done.
 
I

Ian Collins

asimorio said:
Hi all,
If I don't new up a pointer, can I delete it?
see code below:

void A::foo()
{
char* buffer;
delete buffer;

You can try, but your toilet might explode.
 
?

=?ISO-8859-1?Q?Jens_M=FCller?=

asimorio said:
Hi all,
If I don't new up a pointer, can I delete it?
see code below:

void A::foo()
{
char* buffer;
delete buffer;
return;
}

No you can't, the behaviour of your

delete buffer;

is undefined.

What exactly is the purpose of that code?
 
U

uraniumore235

Now lets think about it. When you instantiate a pointer variable, you
obviously want to point it to some defined memory location. Since you
did not new-up or NULL-up a memory location, the pointer basically has
an undefined property. When you delete this pointer variable, which has
an undefined property, you are basically telling the compiler to return
the pointed to location ??? to the heap stack. Does this even make
sense? No!
 
A

asimorio

Well, now i modify a bit my code:
Will it make sense?

void A::Foo(void* a)
{
char* buffer = (char*) a;
delete buffer;
return;
}


regards,
Vynce
 
R

Rolf Magnus

Now lets think about it. When you instantiate a pointer variable, you
obviously want to point it to some defined memory location. Since you
did not new-up or NULL-up a memory location, the pointer basically has
an undefined property. When you delete this pointer variable, which has
an undefined property, you are basically telling the compiler to return
the pointed to location ??? to the heap stack. Does this even make
sense? No!

That reminds me of the Chewbacca defense :)
 
R

Rolf Magnus

asimorio said:
Well, now i modify a bit my code:
Will it make sense?

void A::Foo(void* a)
{
char* buffer = (char*) a;
delete buffer;
return;
}

Yes, as long as A::Foo is only called with a pointer that points to a single
char allocated with new.
 
I

Ian Collins

asimorio said:
Well, now i modify a bit my code:
Will it make sense?

void A::Foo(void* a)
{
char* buffer = (char*) a;
delete buffer;
return;
}
Even if your toilet doesn't explode, you program will surely crash. You
are telling delete to free memory that doesn't belong to the heap.
 
I

Ian Collins

Ian said:
Even if your toilet doesn't explode, you program will surely crash. You
are telling delete to free memory that doesn't belong to the heap.
Oops, didn't see the pointer passed in as a parameter.
 
T

Tomás

asimorio posted:
Hi all,
If I don't new up a pointer, can I delete it?
see code below:

void A::foo()
{
char* buffer;
delete buffer;
return;
}


Think of "new" as a function which returns the address of the object it
creates:

char *new();

Think of "delete" as a function which takes as an argument the address of
the object which was created:

void delete( char * );


The first line of your code defines a local variable without initialising
it. "buffer" contains white noise. It may contain the memory address
70470980, or it may contain the memory address 9327052, or it may contain
the memory address 27696368.

Now you pass this address to "delete". Bottom line is this:

You can only give "delete" an address which was returned from "new",
otherwise it's undefined behaviour.
You can only give "delete []" an address which was returned from "new
[]", otherwise it's undefined behaviour.

Of course, the exception to this is that you can safely give "delete" or
"delete []" a null pointer, and it will have no effect:

delete static_cast<char*>(0);
delete [] static_cast<char*>(0);


-Tomás
 

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
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top