• Advertisement

Archived

This topic is now archived and is closed to further replies.

malloc -> new, realloc -> ???

This topic is 5553 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Just something I got curious about... I know I''m supposed to use ''new'' in C++ rather then malloc. (which makes sense, since it''s a lot easier, typesafe etc. =) However, there does not seem to be a simple to use substitution for realloc. I know, "don''t use realloc, it''s bad will cause memory fragmentation, etc. blabla", but there''s situations where it''s simply impossible to predetermine how much data there''ll be. (Unless you invent a foolproof way to figure out how much userinput you''ll get (statistics? ) My usual way is -new temporaryArray[newSize] -copy old data to temporaryArray -insert new data to temporarArray -delete oldArray -oldArray.pointTo-> temporaryArray Anybody got better ideas, how to speed up the process? (programming, not execution).

Share this post


Link to post
Share on other sites
Advertisement
Use std::vector or std::list. If you're feeling really masocistic, you could use std::list >, but i would have to kill you for coding it



[edited by - Evil Bill on December 5, 2002 10:42:10 AM]

Share this post


Link to post
Share on other sites
Hum, ok, using a linked lists works too...

but let''s say I want a dynamic array for user input
(char*) [no flames about ''use string'' please]
and later on pass the array to some c function
''printf'' etc..., I don''t think it would accept
a list, and conversion, I dunno...

Or maybe I just need the elements to be in one
block of memory, rather then inside a linked list?

Maybe I should try to invent an operator like ''renew''
or something

Share this post


Link to post
Share on other sites
quote:
Original post by Wildfire
However, there does not seem to be a simple to use substitution
for realloc.

That''s because realloc has no meaning in the context of "new". When you use new, you are bringing an object into existence, and it allocates enough space for the object you are creating. Why would you need to change that amount of space? Either you have an object or you don''t.

Share this post


Link to post
Share on other sites
SabreMan: new can also be used to allocate arrays like this:
int *my_dynamic_int_array = new int[100]; 
and you may want to increase the size of this array.

It is true that most of the time using one of the standard containers is enough to handle the problem, but it may sometimes be necessary to manage dynamic arrays by hand. Furthermore, it''s always good know more about programming. (How does std::vector manage it''s memory? Maybe you''ll have to implement your own container class one day...)

Back to the question: as far as I know there''s no realloc equivalent in C++. So you''ll have to new a new buffer, copy your data and remove the new buffer, if you really want to manage dynamic arrays yourself.

Share this post


Link to post
Share on other sites
Ok, maybe I should''ve said ''new[]'' then.
new[] will generate a series, or array of
objects.

And, well, yes there are numerous reasons to
increase the number...
Or do you always work with a single inheritance
of a class? What if you need to generate multiple
of them at a time, and later on add a few more?

Share this post


Link to post
Share on other sites
quote:
Original post by leroy
It is true that most of the time using one of the standard containers is enough to handle the problem, but it may sometimes be necessary to manage dynamic arrays by hand. Furthermore, it''s always good know more about programming. (How does std::vector manage it''s memory? Maybe you''ll have to implement your own container class one day...)

You''re being melodramatic. There''s nothing wrong with using the standard containers. For the very few scenarios you need to handle dynamic reallocation, then you build a custom container for your purposes.
quote:

Back to the question: as far as I know there''s no realloc equivalent in C++.

Actually there is. It''s called "realloc". Anything else wouldn''t be an equivalent.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by Wildfire
Hum, ok, using a linked lists works too...

but let''s say I want a dynamic array for user input
(char*) [no flames about ''use string'' please]
and later on pass the array to some c function
''printf'' etc..., I don''t think it would accept
a list, and conversion, I dunno...



Why not? You use a linked list when you are collecting the data. Later, when you want to start processing the input, you convert the list to a block. Sounds quite reasonable to me

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by leroy
It is true that most of the time using one of the standard containers is enough to handle the problem, but it may sometimes be necessary to manage dynamic arrays by hand. Furthermore, it''s always good know more about programming. (How does std::vector manage it''s memory? Maybe you''ll have to implement your own container class one day...)



I think std::vector uses exactly the "allocate larger array and copy everything" scheme, except that it allocates objects by chunks so that it does not need to reallocate every time.

(By the way, I''m afraid realloc() often does exactly the same...)

Share this post


Link to post
Share on other sites
I didn''t want to sound melodramatic std::* containers are a _good_ thing, but it''s always interesting to know what''s going on under the hood, don''t you think?

Also, isn''t it possible to give the template instantiations of the container classes an allocator object which handles the allocation? Like this:
vector intvec; 
This way one could change the way memory is allocated in the _very_remote_ possibility that one would do so I haven''t played with it yet, so I don''t really know the exact possibilities. But this is getting of topic...

Share this post


Link to post
Share on other sites
Oops, the code part in my previous post is wrong. Sorry about that. Don''t know how to fix it...

Share this post


Link to post
Share on other sites
quote:
Original post by SabreMan
You''re being melodramatic. There''s nothing wrong with using the standard containers. For the very few scenarios you need to handle dynamic reallocation, then you build a custom container for your purposes.



Eh.

MSN

Share this post


Link to post
Share on other sites

  • Advertisement