473,378 Members | 1,623 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

delete op question. do I need to cast?

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
Jul 22 '05 #1
3 1380

"lallous" <la*****@lgwm.org> wrote in message
news:2r*************@uni-berlin.de...
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.
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
Jul 22 '05 #2


I wouldn't use "new" here. Use "malloc" and the rest...
-JKop
Jul 22 '05 #3

"lallous" <la*****@lgwm.org> wrote in message
news:2r*************@uni-berlin.de...
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
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: php newbie | last post by:
I am getting error messages when I try to delete from a table using the values in the table itself. The intent is to delete all rows from TableA where col_2 matches any of the col_1 values. ...
4
by: Richard Lee | last post by:
Hi, I have a question when I do a data type cast. the common way when we do a cast, is we know the type we want to cast to, i.e. we want to cast object to string, object xyz = "question";...
10
by: MaxMax | last post by:
int *p; p = new int; void *q = (void*)p; delete q; Is this valid if p is guaranteed to be a POD? (don't ask why... there is a reason I need this. Clearly I will create p in function, use p...
26
by: Janice | last post by:
What is the major reason for using void*? When should we use void* for both input arguments and return value? How can we cast the void* pointer to the type we need? Thanx
2
by: Rouben Rostamian | last post by:
The main() function in the following code defines an m by n matrix, assigns value(s) to its elements, then passes the matrix to function foo(). For whatever it's worth, I have declared foo() so...
27
by: fermisoft | last post by:
I have a strange problem in my project. I have a class like this...... class CMsg { public: void *m_body; ~CMsg()
1
by: klopter | last post by:
I will describe what I want using the vector class and then point out the limitations of using vector. vector <MyClass> MyContainer. MyContainer.push( new MyClass(...) ) MyContainer now...
3
by: Nindi | last post by:
On comp.lang.c++.moderated http://groups.google.co.uk/group/comp.lang.c++.moderated/browse_thread/thread/8250715711da7760?hl=en the following question was posted ...
2
by: Danielle | last post by:
Hello all - Thank you in advance for any help you are able to provide. I am populating a gridview from a stored procuedure. The returned data is a name, phone number, email and guid of a...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.