470,841 Members | 1,140 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,841 developers. It's quick & easy.

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

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

May 31 '06 #1
4 2085

"asimorio" <th******@gmail.com> wrote in message

| 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;
| }
May 31 '06 #2

"asimorio" <th******@gmail.com> wrote in message
news:11*********************@i39g2000cwa.googlegro ups.com...
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.
May 31 '06 #3
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
May 31 '06 #4
asimorio wrote:
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
May 31 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by foo | last post: by
11 posts views Thread by Squid Seven | last post: by
28 posts views Thread by Wonder | last post: by
7 posts views Thread by Ninan | last post: by
3 posts views Thread by Ramki | last post: by
11 posts views Thread by asimorio | last post: by
29 posts views Thread by Jon Slaughter | last post: by
17 posts views Thread by Nick Keighley | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.