Can you delete a pointer without newing it up? (modified version)

A

asimorio

All,
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
 
S

Sharad Kala

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

It will only be legal if the memory pointed to by a has been obtained by a
call to new operator. Also if the pointer is NULL, then delete has no
effects.

Sharad

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

Jim Langston

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

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

It depends if a was allocated with new or not. If a was allocated with new,
it is legal. If it was not allocated with new, it is undefined and will
most likely cause a memory fault.
 
T

Tomás

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

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


There are two sole conditions under which that would work:

int main()
{
A::Foo( new char );

A::Foo( 0 );
}


-Tomás
 
D

Default User

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

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

What are actually trying to do? What problem are you solving? What you
propose is rarely a good idea.




Brian
 

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,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top