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/BrowseCategoryOrSearchResults.asp?txtCriteria=bcstring&blnWorldDropDownUsed=TRUE&txtMaxNumberOfEntriesPerPage=10&blnResetAllVariables=TRUE&lngWId=3&optSort=Alphabetical

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.

>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.

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/...

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.)

> 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

>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? :icon_rolleyes:

>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.

Yes, follow the link I gave you.

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.

Edited 3 Years Ago by happygeek: fixed formatting

>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.

This article has been dead for over six months. Start a new discussion instead.