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?
Printable View
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?
You would rather use std:vector, 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:
Thanks!
And how about effectiveness of using vector?
Is vector fast and good organized?
Yes. It won't be any slower than if you tried to do everything with new and delete along with trying to call realloc.Quote:
Originally Posted by Funt
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.Quote:
and good 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
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?
Using vector (and re-writing) is worth it.Quote:
Originally Posted by Funt
Do so, and when you face problems get back to us...
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.Quote:
Originally Posted by Siddhartha
The issue is not that you are just doing this. The issue is where you are doing this code.Quote:
Originally Posted by Funt
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:
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.Code:void foo()
{
int *p = new int [10];
if ( something_is_true )
{
perform_some_function();
delete [] p;
}
}
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):
You do this:Code: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;
//...
}
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.Code:#include <vector>
void foo()
{
std::vector<T> p(10);
// resize it to 20;
p.resize(20);
}
Regards,
Paul McKenzie
It is recommended to you for STL gives you a ready-made implementation of a dynamic array.Quote:
Originally Posted by dunakl
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. ;)Quote:
Originally Posted by Siddhartha
You need to explain what exactly you mean... In simple words, that is.Quote:
Originally Posted by dunakl
As far as the thread starter not wanting a dynamic array or a container goes - again, please explain how you arrived at this conclusion... ;)
Quote:
Originally Posted by Siddhartha
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. ;)Quote:
Originally Posted by Funt
Using new[]/realloc/delete[] is a dynamic array, even if the OP didn't call these sequence of operations a "dynamic array".Quote:
Originally Posted by dunakl
Regards,
Paul McKenzie
But what they do want to do is equivalent to what vector does. There really is no difference.Quote:
Originally Posted by dunakl
Using new[] automatically implies you are using the pointer in an array-like manner.Quote:
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. ;)
Regards,
Paul McKenzie