New/delete or malloc/realloc/free

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2006
Posts: 90
Reputation: Brent.tc is an unknown quantity at this point 
Solved Threads: 1
Brent.tc Brent.tc is offline Offline
Junior Poster in Training

New/delete or malloc/realloc/free

 
0
  #1
Sep 17th, 2007
I was wondering which would be better to use for the allocation of memory. The memory that is being allocated will almost certainly have to be reallocated several times.
I am making a string class (how original and I am going to give it dynamic memory allocation (whether or not that is the correct term...)
I actually made one a while back, and it was simply a disgrace, (I had used new/delete method, but in a very crude manner) I have recently recreated the entire thing (from the ground up), and have found malloc/realloc/free to be much more easy to use, and the string class (bcstring) works good now.
If you take experience and ease out of the picture, which would be the better choice to use.
What are some pros of each method?
Some cons?
My code can be found at: http://pscode.com/vb/scripts/BrowseC...t=Alphabetical
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 90
Reputation: Brent.tc is an unknown quantity at this point 
Solved Threads: 1
Brent.tc Brent.tc is offline Offline
Junior Poster in Training

Re: New/delete or malloc/realloc/free

 
0
  #2
Sep 17th, 2007
If the above link does not work, and you have any interest in seeing my code, go to pscode.com search in the c++ category for bcstring and there it will be... I didn't simply post it here because it's pretty long, and it seemed like a waste of space.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,674
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 727
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: New/delete or malloc/realloc/free

 
0
  #3
Sep 17th, 2007
>I am making a string class
If you want it to be as flexible as possible, you'd do something like the standard string class and use an allocator. The default for allocators are new and delete. However, you're free to use whatever you want since this is a class that's completely self-contained. I'd recommend new and delete simply because they're conventional for C++, but there's nothing wrong with malloc and free in this case. You might get some funny looks though.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 90
Reputation: Brent.tc is an unknown quantity at this point 
Solved Threads: 1
Brent.tc Brent.tc is offline Offline
Junior Poster in Training

Re: New/delete or malloc/realloc/free

 
0
  #4
Sep 17th, 2007
The main reason I am not using new and delete, is because 1. I tried that, and failed miserably (mem. allocation failed, and only one class could be made for reasons unknown) and 2. This has worked very well so far, and to resize it I don't need to create a temp to hold the current, then delete, then make new, then set to temp, then delete temp... all I have to do is realloc(whatever).

An allocator?

Are there any real downsides for using malloc/free/...
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: New/delete or malloc/realloc/free

 
0
  #5
Sep 18th, 2007
you might find the following article (about efficient memory management of typed buffers) instructive. i did, when i first read it. reading alexandrescu almost always has that effect.
http://www.ddj.com/cpp/184403806
(the article is failry old; some of the issues it discusses are being addressed in c++0x.)
Last edited by vijayan121; Sep 18th, 2007 at 1:51 am.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 38
Reputation: spankyg is an unknown quantity at this point 
Solved Threads: 0
spankyg's Avatar
spankyg spankyg is offline Offline
Light Poster

Re: New/delete or malloc/realloc/free

 
0
  #6
Sep 18th, 2007
Off the subject, Narue your icon is perfect!!!
Education is what remains after one has forgotten what one has learned in school.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: New/delete or malloc/realloc/free

 
0
  #7
Sep 18th, 2007
> The main reason I am not using new and delete, is because 1. I tried that, and failed miserably
If that is all you did, then there are hidden bugs somewhere else in your code. Simply changing
char *p = new[10];
for
char *p = malloc(10);
in itself should not cause a problem.
Masking the problem is not the same as fixing the problem.

You end up using a different block of memory (most likely), so your previous problems no long have an impact (well, not immediately anyway).

> all I have to do is realloc(whatever).
Except realloc may also have to do what you describe. You're not saving anything, except for one really simple loop in your code.

> _vPtr= realloc(_vPtr,size*charSize);
This is a classic mis-use of realloc bug. If the realloc fails, then you trash your pointer to the memory you already have (this is a memory leak).

Also, malloc.h is NOT a standard include file. You should be including stdlib.h
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,674
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 727
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: New/delete or malloc/realloc/free

 
0
  #8
Sep 18th, 2007
>1. I tried that, and failed miserably (mem. allocation failed,
>and only one class could be made for reasons unknown)
Then you're not using it correctly. Getting it to work will be a good learning experience.

>all I have to do is realloc(whatever).
The design of realloc is questionable, and it's generally best to avoid it even in C. And really, how much are you saving by using it? Two lines, maybe three?

>An allocator?
Yes, follow the link I gave you.

>Are there any real downsides for using malloc/free/...
In this case, aside from getting an ear full from C++ purists, no.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 90
Reputation: Brent.tc is an unknown quantity at this point 
Solved Threads: 1
Brent.tc Brent.tc is offline Offline
Junior Poster in Training

Re: New/delete or malloc/realloc/free

 
0
  #9
Sep 19th, 2007
[quote=Narue;435867
Yes, follow the link I gave you.
[/quote]
I did, but it returns an error.

So, as I understand it I should go ahead and go for new/delete, and I'm guessing I should implement some sort of error checking? (This is an obvious safety measure, but is there any good chance that the allocation fails?... I'm going to implement it anyways, but it won't be as high a priority as if there is a good chance an error happens)

Is there a difference in free store, and the memory allocated by malloc?

---------------------------------------------------
On an ever so different topic, to any of you who are good with algebra (specifically systems of equations, and solving them with determinates (Cramer's rule), or "augmented matrices"), I am working on a project that will parse any valid system of equations, and return the proper variable values (I already have one that will solve for exactly 3 vars, but the equation must be simplified, and it doesn't actually parse the equation... It asks for the numbers that would be placed in the matrices... Obviously this can be very error prone, and time consuming which is why I'm working on the parser.
---
Could you point me to a good, understandable tutorial on systems of equations (not one for college, but instead, one that a 10th grader with very limited knowledge on the subject could easily understand) -- Also, it would be nice if someone could assist in the basics of parsing, because this is what is giving me the most trouble.
---------------------------------------------------
If this is simply too far off topic to post replies here, then email me at brent.tc@hotmail.com
Last edited by Brent.tc; Sep 19th, 2007 at 8:29 pm. Reason: Typo
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,674
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 727
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: New/delete or malloc/realloc/free

 
0
  #10
Sep 19th, 2007
>I did, but it returns an error.
Search whatever reference you want for the <memory> header.

>So, as I understand it I should go ahead and go for new/delete
Why not?

>and I'm guessing I should implement some sort of error checking?
That's generally a good idea, yes.

>Is there a difference in free store, and the memory allocated by malloc?
Free store, heap, dynamic memory, they're all different terms for the same thing.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC