473,397 Members | 1,969 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,397 software developers and data experts.

free() and delete

Hi,

free() and delete are both used to release ptr memory. Any difference.
When shall I use which?

Thanks in advance,
Michael

Jul 31 '06 #1
3 1574

Michael wrote:
Hi,

free() and delete are both used to release ptr memory. Any difference.
When shall I use which?
Use new/delete unless you have a really good reason to use
malloc/realloc/free.

Use delete when you used new, use free when you used malloc.

Jul 31 '06 #2

Michael wrote:
Hi,

free() and delete are both used to release ptr memory. Any difference.
When shall I use which?
Oh, and delete calls a destructor, if any, and free does not.

Jul 31 '06 #3
Michael schrieb:
Hi,

free() and delete are both used to release ptr memory. Any difference.
When shall I use which?
Simple rules:

- Memory allocated with malloc() has to be freed with free(),
- Objects that are new'ed have to be delete'd,
- Arrays of Objects, created with new[], must be destroyed with delete[].

Some guidelines:

- In C++, don't ever use malloc() or free(),
- Instead of new[] you are safer using a container (std::vector<for
example)

--
Thomas
Jul 31 '06 #4

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

Similar topics

16
by: Peter Ammon | last post by:
Often times, I'll have some malloc()'d data in a struct that need not change throughout the lifetime of the instance of the struct. Therefore, the field within the struct is declared a pointer to...
4
by: Office Drone | last post by:
I already figured out how to replace new & delete operators, which wasn't that hard and this is working great globally. However, aside from new & delete, memory allocation is also used via...
3
by: Ramki | last post by:
How does free() , delete() works? If we allocate memory using the following statement int *p = malloc(100*sizeof(int)); Inorder to release the memory we will call '"free(p) " How does...
3
by: chameleon | last post by:
free(0); is there a problem with this? I try it with gcc, but there is no some kind of exception. I wonder if this is accepted from standard C or it is a gcc protection rule. This, work...
3
by: Bartholomew Simpson | last post by:
I am writing some C++ wrappers around some legacy C ones - more specifically, I am providing ctors, dtors and assignment operators for the C structs. I have a ton of existing C code that uses...
1
by: kskkumar | last post by:
when i create an array through malloc, i use free to free it. If i create the same array using new operator (e.g.) int a = new int(20); then i have to user 'delete a'. But i don't use '' in free...
8
by: LeXave | last post by:
Hi all In C++/CLI, is there a way to free an array allocated from an unmanaged dll ? In my managed code, I call a function from that dll which returns me an array of bytes. But how can I free it...
5
by: ycinar | last post by:
Say we have a pointer like below Test * abc = GetValue(); // do something free (abc); // or delete abc; does it matter if you chose delete or free to avoid memory leaks?
3
by: dreiko466 | last post by:
(sorry about my english...) I am a newbie in C (3 month expierience) I have wrote a simple test programm in VS2005, what i do wrong?Please... In this programm i create a double linked list.Then ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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...

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.