Click to See Complete Forum and Search --> : realloc() and new
Funt
September 10th, 2005, 07:45 AM
Hello!
Maybe my question was already discussed.
When we use malloc()/free() functions we have nice routine to reallocate memory that called realloc().
How to increase memory size when new/delete are used?
Siddhartha
September 10th, 2005, 07:56 AM
You would rather use std:vector (http://www.codeguru.com/forum/showthread.php?p=1170316#post1170316), or another appropriate STL Container.
This is a STL Implementation of a Dynamic Array, the size of which can be changed at run-time. You wont have to worry about memory allocations, de-allocations, copying, expansions, etc.
In addition, std::vector <T> comes with many utility member functions, and can also be used with STL Algorithms to give the programmer access to available, tested and reliable functionality.
Here are references that will take you forward:
Article: Vector for Beginners (http://www.codeguru.com/Cpp/Cpp/cpp_mfc/stl/article.php/c4027)
MSDN: std::vector class (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcstdlib/html/vclrfvectorclass.asp)
MSDN: std::vector members (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcstdlib/html/vclrfvectormembers.asp)
<algorithm> (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcstdlib/html/vclrfalgorithm_header.asp)
<algorithm members> (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcstdlib/html/vclrfalgorithmmembers.asp)
Standard Template Library Samples (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcstdlib/html/vcoristandardtemplatelibrarysamples.asp)
Funt
September 10th, 2005, 09:29 AM
Thanks!
And how about effectiveness of using vector?
Is vector fast and good organized?
Paul McKenzie
September 10th, 2005, 09:42 AM
Thanks!
And how about effectiveness of using vector?
Is vector fastYes. It won't be any slower than if you tried to do everything with new and delete along with trying to call realloc.
and good organized?The vector class is the ANSI standard C++ class, so I would think that a lot of thought went into it when it was organized.
Also, the reason why there is no "realloc" for new and delete is because in C++, you have to recreate and copy the entire set of objects. You can't just do a raw memory copy and move for these types of objects. In other words, to do a realloc safely in C++ you have to:
1) Create a new area of memory with new[].
2) Copy the data from the old memory area to the new memory area using "=" in a loop (not memcpy(), since there is no guarantee that the type can use memcpy() to copy the object)
3) Delete the old memory area.
But in general, unless you are not using a standard ANSI C++ compiler, there really is no need for new[]/delete[] for the purpose of creating a dynamic array. The std::vector class takes care of these things, including all memory management, and the "realloc" for vector (which is actually called resize()) is safe and comes for free without you having to code anything.
Regards,
Paul McKenzie
Funt
September 10th, 2005, 09:51 AM
Thanks a lot!
I already have implementation of resizing with new[]/delete[] in way you have described.
In my program I should resize memory only one or few times.
How do you think, should I rewrite my code with "vector" or leave all is?
Siddhartha
September 10th, 2005, 09:53 AM
And how about effectiveness of using vector?
Is vector fast and good organized?A vector can be used as a normal array as well. It is as fast as the programmer can make it... ;)
The first link in my post above (this (http://www.codeguru.com/forum/showthread.php?p=1170316#post1170316)), and the article, and this one (http://www.codeguru.com/forum/showthread.php?p=1229502#post1229502), and many others on this forum and the MSDN links will tell you how to use a vector.
Siddhartha
September 10th, 2005, 09:55 AM
How do you think, should I rewrite my code with "vector" or leave all is?Using vector (and re-writing) is worth it.
Do so, and when you face problems get back to us...
dunakl
September 10th, 2005, 10:04 AM
You would rather use std:vector (http://www.codeguru.com/forum/showthread.php?p=1170316#post1170316), or another appropriate STL Container.
Why the vector or container is recommended? The question does not show the situation for a list of variables/objects or a centralized management to be created. It could be very possible in a general discrete situation.
Paul McKenzie
September 10th, 2005, 10:06 AM
Thanks a lot!
I already have implementation of resizing with new[]/delete[] in way you have described.
In my program I should resize memory only one or few times.
How do you think, should I rewrite my code with "vector" or leave all is?
The issue is not that you are just doing this. The issue is where you are doing this code.
The problem with using new[]/delete[] is that you leave yourself open for memory leaks. What if you forget the delete[] call? This can happen without you realizing it. Look at this code:
void foo()
{
int *p = new int [10];
if ( something_is_true )
{
perform_some_function();
delete [] p;
}
}
See the problem? The delete is conditional, and there you have a memory leak. What if the code is more complex than this? If you have any code that looks like this, with multiple delete[] calls just to make sure you covered all the exit paths, then this is ripe for mismanagement of memory.
Also, if your pointer is a member of a class, this is also another place where memory is mismanaged if you don't code a copy constructor and assignment operator that handles these things.
A vector alleviates all of these problems. There is no need to create a headache on purpose by using new[] and delete[].
As to the resize, it is very simple. Instead of this (assume T is any basic type of type with default constructor):
void foo()
{
T *p = new T [10];
// resize it to 20;
T *pTemp = new T [20];
for (int i = 0; i < 10; ++i )
pTemp[i] = p[i];
delete [] p;
p = pTemp;
//...
}
You do this:
#include <vector>
void foo()
{
std::vector<T> p(10);
// resize it to 20;
p.resize(20);
}
Now which is easier to code and maintain? Not only that, you still have to delete [] p at some point, while with vector, it will delete itself when it goes out of scope.
Regards,
Paul McKenzie
Siddhartha
September 10th, 2005, 10:07 AM
Why the vector or container is recommended? The question does not show the situation for a list of variables/objects or a centralized management to be created. It could be very possible in a general discrete situation.It is recommended to you for STL gives you a ready-made implementation of a dynamic array.
To get a specific answer, you need to ask a specific question... ;)
dunakl
September 10th, 2005, 10:11 AM
It is recommended to you for STL gives you a ready-made implementation of a dynamic array.
To get a specific answer, you need to ask a specific question... ;)
The question is the thread starter did not want a dynamic array, but very possible in a general discrete situation. ;)
Siddhartha
September 10th, 2005, 10:14 AM
The question is the thread starter did not want a dynamic array, but very possible in a general discrete situation. ;)You need to explain what exactly you mean... In simple words, that is.
As far as the thread starter not wanting a dynamic array or a container goes - again, please explain how you arrived at this conclusion... ;)
dunakl
September 10th, 2005, 10:23 AM
You need to explain what exactly you mean... In simple words, that is.
As far as the thread starter not wanting a dynamic array or a container goes - again, please explain how you arrived at this conclusion... ;)
Hello!
Maybe my question was already discussed.
When we use malloc()/free() functions we have nice routine to reallocate memory that called realloc().
How to increase memory size when new/delete are used?
This is a quite general question and does not say he want to create a list of variables, which means that it will be very possible in a general use, not specifically in a situation for handling a list of variable like an array. ;)
Paul McKenzie
September 10th, 2005, 10:26 AM
The question is the thread starter did not want a dynamic arrayUsing new[]/realloc/delete[] is a dynamic array, even if the OP didn't call these sequence of operations a "dynamic array".
Regards,
Paul McKenzie
Paul McKenzie
September 10th, 2005, 10:28 AM
This is a quite general question and does not say he want to create a list of variables,But what they do want to do is equivalent to what vector does. There really is no difference.which means that it will be very possible in a general use, not specifically in a situation for handling a list of variable like an array. ;)Using new[] automatically implies you are using the pointer in an array-like manner.
Regards,
Paul McKenzie
dunakl
September 10th, 2005, 10:33 AM
Using new[]/realloc/delete[] is a dynamic array, even if the OP didn't call these sequence of operations a "dynamic array".
No. The first post did not say the new[]/delete[] stuff. They were introduced by you. ;)
Paul McKenzie
September 10th, 2005, 10:44 AM
No. The first post did not say the new[]/delete[] stuff. They were introduced by you. ;)
??
How to increase memory size when new/delete are used?
Regards,
Paul McKenzie
dunakl
September 10th, 2005, 11:05 AM
hmm..,
How to increase memory size when new/delete are used?
Sorry, I still see it is for a general case, not specifically for list of variables.
;)
Siddhartha
September 10th, 2005, 11:20 AM
No. The first post did not say the new[]/delete[] stuff. They were introduced by you. And... Sorry, I still see it is for a general case, not specifically for list of variables.May be you forgot to read this - I already have implementation of resizing with new[]/delete[] in way you have described.
In my program I should resize memory only one or few times.In any case, when you attempt to solve the problem, you will understand the solutions presented above... ;)
Paul McKenzie
September 10th, 2005, 11:22 AM
What is the difference? Internally, there is no difference. When you call
new T[n];
you are allocating a contiguous block of n sets of T's. When you say
std::vector<T>(n)you are allocating a contiguous block of n sets of T.
The code to std::resize() is roughly equivalent to the code you would need to do with realloc() (by roughly, I mean vector is more efficient, but does the same thing).
Regards,
Paul McKenzie
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.