473,729 Members | 2,150 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1402

"lallous" <la*****@lgwm.o rg> 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.o rg> 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
3614
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. DELETE FROM TableA FROM TableA x INNER JOIN TableA y ON (x.col_1 = y.col_2) Error msg: The table 'TableA' is ambiguous. Can this be done with SQL or should I use T-SQL with cursors here?
4
547
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"; (string)xyz; now we only have a type object of System.String type
10
2247
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 in another function that know what type p is and delete p in a function that for simplicity won't know what p is)
26
2123
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
1932
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 as to make it treat its first argument as a "read-only" object, that is, foo() can read but not alter the matrix. I have a problem, however, with /calling/ foo. If I call foo as foo(a,m,n), my compiler (gcc) complains about:
27
7191
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
1357
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 contains the first instance of MyClass. MyContainer.push( new MyClass(...) )
3
1647
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 ------------------------------------------------------------------------------------------------------- Is this standard conformant to type cast ::opearator delete to a function of type (void (*)(void*)); Is not operator delete already a function of this type?
2
2483
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 contact in our database. I also need to use the Edit and Delete funtions in the gridview. Neither seems to be working as intended.
0
8913
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
9200
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9142
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6722
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6016
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4795
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2677
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2162
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.