| | |
New/delete or malloc/realloc/free
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2006
Posts: 90
Reputation:
Solved Threads: 1
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
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
>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.
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.
•
•
Join Date: Oct 2006
Posts: 90
Reputation:
Solved Threads: 1
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/...
An allocator?
Are there any real downsides for using malloc/free/...
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
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.)
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.
> 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
for
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
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
>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.
>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.
•
•
Join Date: Oct 2006
Posts: 90
Reputation:
Solved Threads: 1
[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
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
>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.
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.
![]() |
Similar Threads
- realloc with new operator (C++)
- Simple string manipulation (C)
- help needed (C++)
- What relation does **indirection operator have with Multidimensional Arrays (C++)
- Malloc Fails (C)
- Another question regarding memory (C)
- How to free up memory used for queues? (C)
- dubble delete (C++)
- type casting (C++)
Other Threads in the C++ Forum
- Previous Thread: TR1 Standard
- Next Thread: Program to output all prime numbers between 3 and 100
| Thread Tools | Search this Thread |
api array arrays based binary c++ c/c++ calculator char char* class classes code coding compile console conversion convert count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






