I'm sorry... You will surely think that I'm a stupid but... I don't understand what you have said.
Every malloc() call can be replaced by an equivalent new |
Sure, true. Nothing to say about it.
But there are no perfect equivalent for realloc. The problem is always this one.
vector<> can be used to manage "arrays" that require to be resized.
I don't understand (your worlds confused me) if vector<> calls constructor if I use a class (live vector<ClassA>). Since now I thinked so.
However. Seeing the fact that BITMAP * objects MUST be inizialized with load_bitmap (or create_bitmap) and then must destroyed with destroy_bitmap, using vector<> directly, is not a solution (need more specific functions, need to set the Allegro functions required to copy an "Image" to a "vector", I need to destroy contained images correctly with destroy_bitmap). So I had the idea of creating a custom "vector", a class, called ImgVec.
I wanted to create a personal "container" (ImgVec) that can contain Allegro Images, can be resized with its own functions (AddImage, AddImgVec, RemoveImage), can call a copy of an image choosed from list (img(ID)) to another pointer, can destroy all bitmap correctly when ImgVec deleted (destructor calls destroy_bitmap function for every BITMAP * and only after that calls free for BITMAP ** with free(Image) ).
So the true "problem" is only the pointer of pointer (BITMAP **) that must be resizable. So the better solution seems to me to use malloc/realloc/free (can resize, I avoid to use useless data that costs space, I need only reallocation and for this reason vector<> seem not so necessary).
Did I do some mistake?
It does not matter how the library uses the memory; vector's specification is such that it is functionally equivalent to an array to the user. |
Ok. I think to have understanded this (more or less).
However, thinking about this specific enviroment, I don't need all the "functions" of a vector. Also using malloc I can create "arrays". Surely "vector" will require memory to have access to its own functions that, at the end, will be no accessible thinking about the fact that the BITMAP "array" of my class must be private. I have also my own functions for count(), add(), remove() so I'll have an useless "duplication" if I use vector<>. So, in this case, it seems to me not so efficent to use vector<>.
But, perhaps I'm wrong...
vector<> uses malloc and free because of its requirements |
More or less is the same for my class (surely I cannot compare my little class with the power of stl containers).
new and malloc differ only in that new also runs the constructor |
Ok. But I uses malloc for a pointer of pointer of a struct (so NO constructors to be worried about)
vector<> does NOT want to run constructors in the case you mentioned; hence it MUST use malloc. |
This last think confuses me. I didn't understand it (perhaps becouse of my bad English), sorry
-----
I know that Zaita is an expert (unlike me).
However I only try to demonstrate (with some WANTED exaggerations) that it is not so true that in C++ is SO FORBIDDEN to use malloc/realloc (becouse also vectors<> use them and becouse also some libraries used both in C, C++ can use them). Yes, it is a risk to use malloc/realloc/free becouse are not so raccomended in C++ becouse its object-oriented approach (unlike C). But, perhaps becouse I'm not an expert, I cannot see any problem to use malloc/realloc/free also in C++ if used consciously.