Connecting Tech Pros Worldwide Forums | Help | Site Map

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

asimorio
Guest
 
Posts: n/a
#1: May 31 '06
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


Sharad Kala
Guest
 
Posts: n/a
#2: May 31 '06

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



"asimorio" <thenghui@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;
| }


Jim Langston
Guest
 
Posts: n/a
#3: May 31 '06

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



"asimorio" <thenghui@gmail.com> wrote in message
news:1149068444.568615.39530@i39g2000cwa.googlegro ups.com...[color=blue]
> 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;
> }[/color]

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.


Tomás
Guest
 
Posts: n/a
#4: May 31 '06

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


asimorio posted:
[color=blue]
> 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;
> }[/color]


There are two sole conditions under which that would work:

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

A::Foo( 0 );
}


-Tomás
Default User
Guest
 
Posts: n/a
#5: May 31 '06

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


asimorio wrote:
[color=blue]
> 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;
> }[/color]

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




Brian
Closed Thread