 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Gordon Ellsworth Guest
|
Posted: Thu Mar 03, 2005 10:35 pm Post subject: reallocate memory for arrays using new[] and delete[] |
|
|
In C, if you make an array, you can always resize it like this:
struct mystruct
{
int i;
};
int main(int argc, char **argv)
{
int g = 0;
struct mystruct *myarray = NULL;
for(g = 0; g < 5; ++g)
{
myarray = realloc(myarray, g * sizeof(struct mystruct));
memset(&myarray[g], 0, sizeof(struct mystruct));
myarray[g].i = g;
}
return 0;
}
Is there a way to do this in C++ using new[] and delete[] without
having to create a new array of larger size, copy all the elements to
the new array, and then delete the old array (I call it memory
thrashing)?
Sincerely,
Gordon E.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Maciej Sobczak Guest
|
Posted: Sat Mar 05, 2005 4:21 am Post subject: Re: reallocate memory for arrays using new[] and delete[] |
|
|
Hi,
Gordon Ellsworth wrote:
| Quote: | In C, if you make an array, you can always resize it like this:
[...]
for(g = 0; g < 5; ++g)
{
myarray = realloc(myarray, g * sizeof(struct mystruct));
memset(&myarray[g], 0, sizeof(struct mystruct));
|
No, you cannot. In the first go, g==0 and the second parameter to
realloc is 0 as well - you cannot use the pointer returned by realloc to
access the memory. This pointer may be NULL and even if it's not,
accessing the memory is forbidden.
| Quote: | Is there a way to do this in C++ using new[] and delete[] without
having to create a new array of larger size, copy all the elements to
the new array, and then delete the old array (I call it memory
thrashing)?
|
No (you cannot do this in C++ *using* new[] and delete[]).
But you can use std::vector, it has the resize (and reserve) member
funcitons.
--
Maciej Sobczak : http://www.msobczak.com/
Programming : http://www.msobczak.com/prog/
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Gerhard Menzl Guest
|
Posted: Sat Mar 05, 2005 4:26 am Post subject: Re: reallocate memory for arrays using new[] and delete[] |
|
|
Gordon Ellsworth wrote:
| Quote: | In C, if you make an array, you can always resize it like this:
struct mystruct
{
int i;
};
int main(int argc, char **argv)
{
int g = 0;
struct mystruct *myarray = NULL;
for(g = 0; g < 5; ++g)
{
myarray = realloc(myarray, g * sizeof(struct mystruct));
memset(&myarray[g], 0, sizeof(struct mystruct));
myarray[g].i = g;
}
return 0;
}
|
There are two things that are wrong with this code. First, it does not
check the pointer returned by realloc() against NULL. Second, the memory
occupied by the array is never freed. You may argue that this does not
really matter in a toy example, but you should note that none of these
deficiencies would have crept in in equivalent C++ code even if hastily
sketched (see below).
By the way, what is the purpose of the gratuitous call to memset? The
nulled memory is overwritten in the next statement anyway.
| Quote: | Is there a way to do this in C++ using new[] and delete[] without
having to create a new array of larger size, copy all the elements to
the new array, and then delete the old array (I call it memory
thrashing)?
|
Is there a particular reason why
#include
struct mystruct { int i; };
int main ()
{
std::vector<mystruct> myvector;
int const maxg = 5;
myvector.reserve (maxg);
for (int g = 0; g < maxg; ++g)
{
mystruct ms;
ms.i = g;
myvector.push_back (ms);
}
}
doesn't work for you?
Of course, if you don't have any idea how much space you are going to
need beforehand, realloc() won't save you from "memory thrashing"
either. When there is no room beyond the end, it's allocate-and-copy anyway.
--
Gerhard Menzl
#dogma int main ()
Humans may reply by replacing the thermal post part of my e-mail address
with "kapsch" and the top level domain part with "net".
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Andrew Koenig Guest
|
Posted: Sat Mar 05, 2005 4:57 am Post subject: Re: reallocate memory for arrays using new[] and delete[] |
|
|
"Gordon Ellsworth" <gordone (AT) anora (DOT) org> wrote
| Quote: | In C, if you make an array, you can always resize it like this:
struct mystruct
{
int i;
};
int main(int argc, char **argv)
{
int g = 0;
struct mystruct *myarray = NULL;
for(g = 0; g < 5; ++g)
{
myarray = realloc(myarray, g * sizeof(struct mystruct));
memset(&myarray[g], 0, sizeof(struct mystruct));
myarray[g].i = g;
}
return 0;
}
Is there a way to do this in C++ using new[] and delete[] without
having to create a new array of larger size, copy all the elements to
the new array, and then delete the old array (I call it memory
thrashing)?
|
The usual way to solve this problem in C++ is to use a vector:
vector
for (g = 0; g < 5; ++g) {
mystruct m;
m.i = g;
mystruct.push_back(m);
}
Using new[] and delete[] for such purposes is just making things more
difficult for yourself by replicating functionality that the standard
library already has.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Lars Tetzlaff Guest
|
Posted: Sat Mar 05, 2005 10:10 am Post subject: Re: reallocate memory for arrays using new[] and delete[] |
|
|
Gordon Ellsworth wrote:
| Quote: | In C, if you make an array, you can always resize it like this:
.... |
| Quote: |
Is there a way to do this in C++ using new[] and delete[] without
having to create a new array of larger size, copy all the elements to
the new array, and then delete the old array (I call it memory
thrashing)?
|
realloc does this "memory trashing"
try something like this:
#include <vector>
struct mystruct
{
int i;
};
int main(int argc, char **argv)
{
struct mystruct s;
std::vector<struct mystruct> myarray;
for( s.i = 0; s.i < 5; ++s.i )
{
myarray.push_back( s );
}
return 0;
}
This works much like realloc.
If you do not need a contiguous chunk of memory, better try a deque .
Lars
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
R Samuel Klatchko Guest
|
Posted: Fri Mar 18, 2005 1:14 pm Post subject: Re: reallocate memory for arrays using new[] and delete[] |
|
|
Gerhard Menzl wrote:
| Quote: | int main(int argc, char **argv)
{
int g = 0;
struct mystruct *myarray = NULL;
for(g = 0; g < 5; ++g)
{
myarray = realloc(myarray, g * sizeof(struct mystruct));
memset(&myarray[g], 0, sizeof(struct mystruct));
myarray[g].i = g;
}
return 0;
}
There are two things that are wrong with this code. First, it does not
check the pointer returned by realloc() against NULL. Second, the memory
occupied by the array is never freed. You may argue that this does not
really matter in a toy example, but you should note that none of these
deficiencies would have crept in in equivalent C++ code even if hastily
sketched (see below).
|
There is a third problem as well which often happens when using realloc.
You should never use the form "x = realloc(x, ...)" because in the
case that realloc fails, you will have lost the pointer to the already
allocated memory which will cause a leak. When using realloc, you
should always use the form:
n = realloc(x, ...);
if (n == 0)
{
// could not grow memory
// x still points to allocated memory so make
// sure it doesn't get leaked
}
x = n;
or perhaps:
save = x;
x = realloc(x, ...);
or a couple of other variations but you get the point.
samuel
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|