hi all!! I need to create a function that by reference reallocate an asymmetrical bidimensional
character matrix...
I've done something like that but it crash...
thank you for your many replies!
sorry for the CODE tag... I didn't find it :P
quoting lindley for the behaving of the realloc.
I used realloc instead new/delete beacause with the C++ operator you can't "realloc" memory unless you create another memory area of the same size of the one you want to realloc to be able to store your data and then re-copy in the "resized" original "area".
basically I can't see to much difference from the C operator and the C++ operator to manage the memory (talking about char type) so I thought I'd better use realloc instead creating a self-made one with new/delete.
I think it doesn't matter if I use C or C++. the only thing to pay attention for is use only malloc/realloc/free or new/delete.
for the goal i'm trying to reach i think I can do the same thing with the C dynmem stuff or the C++ dynmem stuff...
about this line:
i made it for keeping the memory contiguous.
anyway all this code work only for the first element...
p.s. I'd prefer a C style solution for the problem... i feel it much lower and manageable instead a C++ style one.
interesting solution... I stopped studing C++ after class... so i miss all the strange template stuff, the std library, and so on... good excuse for getting an upgraded!!! many thanks!!!
(it's still be interesting viewing the realloc version :P)
Originally Posted by Paul McKenzie
Why? What are the reasons for this requirement?
Regards,
Paul McKenzie
don't use delete for unlocating an area allocated by malloc/calloc/realloc and don't "free()" an area allocated by new because they manage memory in different ways!
Last edited by EvIl_DeViL; July 31st, 2009 at 01:09 PM.
(it's still be interesting viewing the realloc version :P)
Same code sample.
Vector will automatically expand as required.
Great stuff!
If my post was interesting or helpful, perhaps you would consider clicking the 'rate this post' to let me know (middle icon of the group in the upper right of the post).
I used realloc instead new/delete beacause with the C++ operator you can't "realloc" memory unless you create another memory area of the same size of the one you want to realloc to be able to store your data and then re-copy in the "resized" original "area".
A good reason, right up until you realize that std::vector::resize() is C++'s answer to realloc----plus it makes things even easier by completely hiding the fact that you're working with dynamic memory at all.
Unless you're working with strings specifically, in which case std::string has some behaviors which are more intuitive than std::vector<char>.
interesting solution... I stopped studing C++ after class... so i miss all the strange template stuff, the std library, and so on... good excuse for getting an upgraded!!! many thanks!!!
(it's still be interesting viewing the realloc version :P)
There are several threads that have full source to routines that create dynamically allocated 2D arrays.
don't use delete for unlocating an area allocated by malloc/calloc/realloc and don't "free()" an area allocated by new because they manage memory in different ways!
What I meant by "what were your reasons for this requirement" is to see why you needed to create a multidimensional array using 'C' methods instead of just using vector<string>.
Bookmarks