Topic: remalloc as new operator
Author: wl1807@sarah.albany.edu (William F. Laviolette Jr.)
Date: 27 Oct 91 15:45:58 GMT Raw View
Hello all! My question is this. Is there a new operator or some
other method for perfoming the "c" realloc command in "c++"? Since it
is suggested that you should not mix malloc and new, would that rule out
overloading new and writing it using realloc.
Say I call "arr = new int [100000]" and fill all the elements in arr, now
I want arr to be larger, say 10,000,000 elements. How is this done.?
--
+------------------------------------------------------------------------+
| _________ | William F. Laviolette Jr. |
|(( (_) ) ) o / / ))| State University of New York |
| / / / / | at Albany |
| ====', / / / / / | wl1807@sarah.albany.edu |
| ( / / / / / / / | wflavio@nysnet.ny.gov |
| \___/______/ __(__(__(__(___/ |--------------------------------|
| ____/_____) | "Common sense is genius dressed|
| "Billy" ( / | in its working clothes." |
| \__/ | -Ralph Waldo Emerson |
+------------------------------------------------------------------------+
Author: tmb@ai.mit.edu (Thomas M. Breuel)
Date: 28 Oct 91 01:53:30 GMT Raw View
In article <1991Oct27.154558.2579@sarah.albany.edu> wl1807@sarah.albany.edu (William F. Laviolette Jr.) writes:
Hello all! My question is this. Is there a new operator or some
other method for perfoming the "c" realloc command in "c++"? Since it
is suggested that you should not mix malloc and new, would that rule out
overloading new and writing it using realloc.
Say I call "arr = new int [100000]" and fill all the elements in arr, now
I want arr to be larger, say 10,000,000 elements. How is this done.?
You would write the following code:
int *old_arr=arr;
arr=new int[10000000];
for(int i=0;i<100000;i++) arr[i]=old_arr[i];
delete old_arr;
That's essentially what realloc has to do for growing data structures
anyway (unless they happen to be at the end of the heap and your
implementation takes advantage of this fact). Note that this loop may
do something more complicated than just copy the data bitwise.
Copying the data bitwise (as realloc does) may be incorrect for C++
data structures, since it does not use user-defined initialization
and assignment operators.
"realloc" is usually more useful for shrinking allocated data. A lot
of old C code allocates a large array, reads data into it, and then
uses realloc to shrink it to the number of elements actually used
(returning the unused portion to the storage allocator). It is
questionable whether this is good style in C++, and properly
encapsulated data structures that are hybrids between arrays and lists
(e.g., a list of blocks) are probably a better compromise if you don't
want to pay the cost for the extra copy.
Thomas.
Author: markt@ersys.edmonton.ab.ca (Mark Tarrabain)
Date: 28 Oct 91 02:11:49 GMT Raw View
wl1807@sarah.albany.edu (William F. Laviolette Jr.) writes:
>
> Hello all! My question is this. Is there a new operator or some
> other method for perfoming the "c" realloc command in "c++"? Since it
> is suggested that you should not mix malloc and new, would that rule out
> overloading new and writing it using realloc.
> Say I call "arr = new int [100000]" and fill all the elements in arr, now
> I want arr to be larger, say 10,000,000 elements. How is this done.?
> --
[long sig deleted]
The saying goes: "You can't get there from here". This is true when
talking about reallocating memory that was originally allocated with new.
The only way I know of around this is to roll your own new and delete
operators (not a horribly difficult task, but still a pain). I haven't
heard of there being any intention to add a ``renew'' operator to C++
before its standardization. (I could be wrong, however, feel free to
shout nasty things in my direction if I am... I would be quite happy to
know that I am wrong, actually.)
Mark Tarrabain markt@ersys.edmonton.ab.ca
Edmonton Remote Systems: Serving Northern Alberta since 1982
Author: hf@informatik.uni-kl.de (Harald Fuchs)
Date: 28 Oct 91 11:46:01 GMT Raw View
In article <TMB.91Oct27205330@volterra.ai.mit.edu> tmb@ai.mit.edu (Thomas M. Breuel) writes:
You would write the following code:
int *old_arr=arr;
arr=new int[10000000];
for(int i=0;i<100000;i++) arr[i]=old_arr[i];
delete old_arr;
That's fine for ints. If you replace int by a type T with an expensive
constructor, it's inefficient: you call the default constructor fort
100000 Ts, and immediately afterwards you throw the objects away by
calling the assignment operator. In this case you might want to call
the copy constructor:
T* old_arr = arr;
arr = (T*) new char [sizeof (T) * 1000000];
for (int i = 0; i < 100000; i++) new (&arr[i]) T (old_arr[i]);
while (i < 1000000) new (&arr[i++]) T;
delete[] old_arr;
--
Harald Fuchs <hf@informatik.uni-kl.de>
Author: thorson@typhoon.atmos.colostate.edu (Bill Thorson)
Date: 29 Oct 91 04:29:47 GMT Raw View
In article <1991Oct27.154558.2579@sarah.albany.edu> wl1807@sarah.albany.edu (William F. Laviolette Jr.) writes:
>
> Hello all! My question is this. Is there a new operator or some
>other method for perfoming the "c" realloc command in "c++"? Since it
>is suggested that you should not mix malloc and new, would that rule out
>overloading new and writing it using realloc.
>Say I call "arr = new int [100000]" and fill all the elements in arr, now
>I want arr to be larger, say 10,000,000 elements. How is this done.?
>--
Is this his only solution? Do you'all think that there should be
a new feature added called 'renew'? I could sure use it.
Bill Thorson
--
#!/bin/sh
#-----------------------------------------------------------------------#
echo Bill Thorson thorson@typhoon.atmos.colostate.edu
echo Dept of Atmospheric Science +1 303 491-8339
echo Colorado State University
echo Ft. Collins, CO 80523
#-----------------------------------------------------------------------#
Author: dennisg@tti.UUCP (Dennis P. Glatting)
Date: 30 Oct 91 11:41:11 GMT Raw View
In article <1991Oct27.154558.2579@sarah.albany.edu> wl1807@sarah.albany.edu
(William F. Laviolette Jr.) writes:
>
> Hello all! My question is this. Is there a new operator or some
> other method for perfoming the "c" realloc command in "c++"? Since it
> is suggested that you should not mix malloc and new, would that rule out
> overloading new and writing it using realloc.
> Say I call "arr = new int [100000]" and fill all the elements in arr, now
> I want arr to be larger, say 10,000,000 elements. How is this done.?
there is a interesting little problem with realloc(). I ran into this problem
using the Computer Innovations compiler under QNX. QNX runs on 80x86
processors.
it seems that when a block is realloc()'d your not necessary going to get a
block of memory back at the same address but its original contents will be
there. this has caused me some grief but more importantly taught me something
i didn't know.
page 348, C A Reference Manual, Harbison & Steele.
--
...!uunet!tti!dennisg | Threaded Technologies, Inc.
dennisg@tti.UUCP | Loveland, Ohio