C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

delete op question. do I need to cast?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
lallous
Guest





PostPosted: Mon Sep 27, 2004 9:32 am    Post subject: delete op question. do I need to cast? Reply with quote



Hello

Consider this:

// allocate a buffer, and cast to MYSTRUCT
MYSTRUCT *p = (MYSTRUCT *) new char[sizeof(MYSTRUCT) + 100];

// free the buffer

// 1. can I free directly as:
delete [] p;

// 2. or as:
delete p;

// 3. or better, to cast to char* and delete, as:
delete [] (char *)p;

Which is correct (if any)?

The struct doesn't have any dtor or ctor() it is just a plain C struct.

--
Elias


Back to top
JKop
Guest





PostPosted: Mon Sep 27, 2004 10:01 am    Post subject: Re: delete op question. do I need to cast? Reply with quote





I wouldn't use "new" here. Use "malloc" and the rest...


-JKop
Back to top
John Harrison
Guest





PostPosted: Mon Sep 27, 2004 10:01 am    Post subject: Re: delete op question. do I need to cast? Reply with quote




"lallous" <lallous (AT) lgwm (DOT) org> wrote

Quote:
Hello

Consider this:

// allocate a buffer, and cast to MYSTRUCT
MYSTRUCT *p = (MYSTRUCT *) new char[sizeof(MYSTRUCT) + 100];

// free the buffer

// 1. can I free directly as:
delete [] p;

// 2. or as:
delete p;

// 3. or better, to cast to char* and delete, as:
delete [] (char *)p;

Which is correct (if any)?


3 is correct.

Quote:
The struct doesn't have any dtor or ctor() it is just a plain C struct.

That makes no difference as far as correctness goes.

john



Back to top
Timothy Madden
Guest





PostPosted: Mon Sep 27, 2004 10:04 am    Post subject: Re: delete op question. do I need to cast? Reply with quote


"lallous" <lallous (AT) lgwm (DOT) org> wrote

Quote:
Hello

Consider this:

// allocate a buffer, and cast to MYSTRUCT
MYSTRUCT *p = (MYSTRUCT *) new char[sizeof(MYSTRUCT) + 100];

// free the buffer

// 1. can I free directly as:
delete [] p;

// 2. or as:
delete p;

// 3. or better, to cast to char* and delete, as:
delete [] (char *)p;

Which is correct (if any)?

Of course
delete [] (char *)p;
is correct.
Use typecasts only when necessary. They bypass the type-checking rules of
the language.
Also if you have some dlls take care to delete from the same module where
you allocated the data with new.

Timothy Madden
Romania
-----------------------------------------------
And I don't wanna miss a thing



Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.