473,404 Members | 2,137 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,404 software developers and data experts.

What is the difference between free and delete?

39
Say we have a pointer like below

Expand|Select|Wrap|Line Numbers
  1. Test * abc = GetValue();
  2. // do something
  3. free (abc);
  4. // or 
  5. delete abc;
does it matter if you chose delete or free to avoid memory leaks?

thanks
Jan 15 '08 #1
5 9568
zonar00
16
Say we have a pointer like below

Expand|Select|Wrap|Line Numbers
  1. Test * abc = GetValue();
  2. // do something
  3. free (abc);
  4. // or 
  5. delete abc;
does it matter if you chose delete or free to avoid memory leaks?

thanks
as far as i know the ans lies on how these two functions are used independently of each other.like free only ensure that the space oocuiped by the varaible is made re-initalized (emptied) and is available for further use. Like
{12,222,334,34}={0,0,0,0)

where as delete would delete the entire memory space occupied making it imposible to refer the varaiable once its gets deleted where in free u could still access it.
Jan 15 '08 #2
mac11
256 100+
free and delete both do the same sort of thing - that is, reclaim dynamically allocated memory. There are significant differences but the quick and dirty rule is to use free to deallocate memory which you allocated using malloc - and to use delete to deallocate memory which you've allocated using new.

you can google for "c++ free" and "c++ delete" for all the gory details -there is no need for me to type them out here.
Jan 15 '08 #3
Ganon11
3,652 Expert 2GB
It's as simple as this.

If the memory the pointer points to was allocated with malloc(), calloc(), or any function like that, you must use free().

If the memory the pointer points to was allocated with the new operator, you must use delete.

If you are working in C++, there is no reason to use malloc(), calloc(), realloc(), etc. so there should be no reason to use free().
Jan 15 '08 #4
Savage
1,764 Expert 1GB
And further more,delete calls a object destructor(if allocated variable is a object)
which deallocates memory used by object members(if there is some memory,and if you have coded the destructor well).Similar thing is with new,it calls object constructor...(etc.)
Jan 15 '08 #5
weaknessforcats
9,208 Expert Mod 8TB
And further, malloc() and free() use the C Run-time heap (CRT heap). Whereas new and delete may be overloaded on a class basis to use a private heap.

Private heaps are cool since you just delete the heap when you are done and that avoids the leaks, period.

So, you could new an object, use free() to release heap allocations and the free() would crash the program since the allocations weren't made in the CRT heap.

malloc() and free() are part of C. C++ is not C. Do not use these things.
Jan 15 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Sid | last post by:
Hi, I was going through this reference code and playing around with it. The code (shown below) shows a class that represents a stack of string pointers In the code below in particular in the...
2
by: hvaisane | last post by:
Valgrind says ==11604== Invalid read of size 4 ==11604== at 0x8048ABB: main (foo.cc:36) ==11604== Address 0x1B92415C is 4 bytes inside a block of size 8 free'd ==11604== at 0x1B90514F:...
7
by: bluekite2000 | last post by:
new/delete, std::allocator, pool_allocator or mt_allocator??? What r the pros and cons if each method? PS: I m designing a matrix/tensor/vector lib, all of which call base_constructor of class...
24
by: wwj | last post by:
Hi ,all I want to know the difference between char a and char *p=new char and the difference between the heap and the stack ,and if the char a is corresponding to the stack in MEMORY,and char...
2
by: Viktor Popov | last post by:
Hi, I am tryind to delete from a table using a Stored Procedure, but I don't know how to do the following: The DELETE STATEMENT must be: DELETE FROM MESSAGES WHERE MSSGID IN (@MSGIDS)
2
by: WittyGuy | last post by:
Is there any difference between the following two snippets of code? snippet1: int main() { int* p = new int(5); // With argument for initialising p to the value 5 .... .... ....
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
350
by: Lloyd Bonafide | last post by:
I followed a link to James Kanze's web site in another thread and was surprised to read this comment by a link to a GC: "I can't imagine writing C++ without it" How many of you c.l.c++'ers use...
15
by: Pranav | last post by:
class just{ public : int x; just(){ x=1234; } just(int i){ x = i;} ~just(){ cout << " Here We Are\n" ;} }; int main() {
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...

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.