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

when to use new/delete instead of malloc/free

Hi,

I just want to know... how i decide on when to use new/delete and when malloc/free.?

Thanks :)
Dec 28 '07 #1
3 2116
zlash
9
new and delete are C++ exclusive keywords so if you are coding in C you have to stick with malloc and free.

I dont know if there is really a difference between using new/delete and malloc/free in C++ but the first pair is easier and cleaner to use, because you dont have to deal with the size of the memory required in the case of classes and structs.

One thing that is very important, is that memory allocated by new and malloc is not necessarily compatible, so you have to check how the memory was allocated before using delete/free. For example, strdup() uses malloc for the duplicated string, so you should use free() in this case.

Hope it helped you and sorry for my english.
Dec 28 '07 #2
oler1s
671 Expert 512MB
I dont know if there is really a difference between using new/delete and malloc/free in C++
There is. New/delete handle the construction/destruction of C++ objects, as opposed to the just get/free a block of memory in C. It's not unusual for new/delete to reuse malloc/free under the hood, with perhaps a bit of extra code, but that's not something you deal with. In C++, new/delete it is.

For example, strdup() uses malloc for the duplicated string, so you should use free() in this case.
Correct. But strdup happens to be nonstandard, and besides, in C++, you would be using the STL string class anyway.

Just further clarifications on my part.
Dec 28 '07 #3
new and delete are C++ exclusive keywords so if you are coding in C you have to stick with malloc and free.

I dont know if there is really a difference between using new/delete and malloc/free in C++ but the first pair is easier and cleaner to use, because you dont have to deal with the size of the memory required in the case of classes and structs.

One thing that is very important, is that memory allocated by new and malloc is not necessarily compatible, so you have to check how the memory was allocated before using delete/free. For example, strdup() uses malloc for the duplicated string, so you should use free() in this case.

Hope it helped you and sorry for my english.
Thank you for your answer. Your english was perfect just like your answer :)
Thanks a lot for taking out time and answering my question. Have a great new year!
Dec 29 '07 #4

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

Similar topics

3
by: Woodster | last post by:
I knoew that free is usually paired with malloc and delete is usually paired with malloc, alloc or similar. Can I use delete with malloc? The main reason I ask is for using the strdup function....
9
by: onsbomma | last post by:
Hello I know this is not good code, but i was wondering why dubble delete gives an error (not an exception!!) and dubble free doesn't?? If new/delete works like malloc/free. Is the error caused...
15
by: Roy Smith | last post by:
I understand that "delete xp" deletes a scalar object and "delete xp" deletes an array of objects, but what I don't understand is why you need to tell the compiler which you're doing. When you...
2
by: Dave | last post by:
Hello all, I'd like to find a source on the web that discusses, in a comprehensive manner and in one place, everything about new / delete. It should include overloading operator new, the new...
15
by: fix | last post by:
Hi all, I am writing a program using some structs, it is not running and I believe it is because there's some memory leak - the debugger tells me that the code causes the problem is in the malloc...
3
by: silver360 | last post by:
Hello, I'm trying to create a basic Heap manager and i have some question about new/delete overloading. The following code give me this output : >> $./heap >> registered : 0x804d098 >>...
5
by: mkaushik | last post by:
Hi everyone, Im just starting out with C++, and am curious to know how "delete <pointer>", knows about the number of memory locations to free. I read somewhere that delete frees up space...
12
by: yufufi | last post by:
Hello, How does delete know how much memory to deallocate from the given pointer? AFAIK this informations is put there by new. new puts the size of the allocated memory before the just before...
58
by: Jorge Peixoto de Morais Neto | last post by:
I was reading the code of FFmpeg and it seems that they use malloc just too much. The problems and dangers of malloc are widely known. Malloc also has some overhead (although I don't know what is...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
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,...

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.