473,320 Members | 1,976 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

realloc vs vector

I've got an array in the vein of

MyType* array;
arry = new MyType[100];

This of course works fine, but I have the need to dynamically grow the
boundaries of that array at runtime. I can't seem to use std ::
vector or any other container, cause I've got two more "lists" which
keep pointers to the elements of `array'.

So, if I used std :: vector in order to resize `array', both lists of
pointers would contain garbage, given the resize operation of the
container would shift the memory around - something which I can hardly
check statically.

So what is a good way of dynamically resizing an array without having
to rearrange the internal memory representation? Or, am I on the
totally wrong track here... (might well be). I thought of using
malloc and realloc, but then these do not call the constructors and
destructors of MyType.

Andi.
Jul 22 '05 #1
6 5277

"Andreas Bauer" <ba*****@mailbroy.informatik.tu-muenchen.de> wrote in
message news:sl********************@localhost.localdomain. ..
I've got an array in the vein of

MyType* array;
arry = new MyType[100];

This of course works fine, but I have the need to dynamically grow the
boundaries of that array at runtime. I can't seem to use std ::
vector or any other container, cause I've got two more "lists" which
keep pointers to the elements of `array'.

So, if I used std :: vector in order to resize `array', both lists of
pointers would contain garbage, given the resize operation of the
container would shift the memory around - something which I can hardly
check statically.

So what is a good way of dynamically resizing an array without having
to rearrange the internal memory representation? Or, am I on the
totally wrong track here... (might well be). I thought of using
malloc and realloc, but then these do not call the constructors and
destructors of MyType.

Andi.


The answer is very simple, don't keep pointers to the 'array'. This will
cause problem whatever means of resizing the array you use, malloc/realloc,
new, vector, its all the same.

Instead keep integer indexes in your lists, these will always be valid.

john
Jul 22 '05 #2

"Andreas Bauer" <ba*****@mailbroy.informatik.tu-muenchen.de> schrieb im
Newsbeitrag news:sl********************@localhost.localdomain. ..
I've got an array in the vein of

MyType* array;
arry = new MyType[100];
So, if I used std :: vector in order to resize `array', both lists of
pointers would contain garbage, given the resize operation of the
container would shift the memory around - something which I can hardly
check statically.


You could use a std::deque, where references to items are not invalid after
a growth.
Of course a deque is not an dynamic array its more a dynamic array of
pointers to the stored elements.
(Thats why the references to items can stay valid during growth)
Regards
Michael
Jul 22 '05 #3
"Andreas Bauer" <ba*****@mailbroy.informatik.tu-muenchen.de> wrote in
message news:sl********************@localhost.localdomain. ..
I've got an array in the vein of MyType* array;
arry = new MyType[100]; This of course works fine, but I have the need to dynamically grow the
boundaries of that array at runtime. I can't seem to use std ::
vector or any other container, cause I've got two more "lists" which
keep pointers to the elements of `array'. So, if I used std :: vector in order to resize `array', both lists of
pointers would contain garbage, given the resize operation of the
container would shift the memory around - something which I can hardly
check statically. So what is a good way of dynamically resizing an array without having
to rearrange the internal memory representation? Or, am I on the
totally wrong track here... (might well be). I thought of using
malloc and realloc, but then these do not call the constructors and
destructors of MyType.


It wouldn't work anyway, because realloc would invalidate those pointers as
surely as std::vector would.

If you must have pointers to your container elements, rather than integers
that represent array indices, have you considered std::deque instead? It
has the property of not moving elements around as long as you restrict
yourself to inserting and deleting elements at either end of the sequence,
rather than in the middle.
Jul 22 '05 #4
"Andreas Bauer" <ba*****@mailbroy.informatik.tu-muenchen.de> wrote in
message
MyType* array;
arry = new MyType[100];

This of course works fine, but I have the need to dynamically grow the
boundaries of that array at runtime. I can't seem to use std ::
vector or any other container, cause I've got two more "lists" which
keep pointers to the elements of `array'.
In addition to std::deque as others pointed out, you can also try std::list.
This one lets you insert elements even in the middle of the list, and
pointers to existing elements remain valid!

So what is a good way of dynamically resizing an array without having
to rearrange the internal memory representation? Or, am I on the
totally wrong track here... (might well be). I thought of using
malloc and realloc, but then these do not call the constructors and
destructors of MyType.


Note that realloc is not garaunteed to work. If the array can be grown in
place, that is if the memory is available, then yes it will work (ie.
pointers to existing elements will remain valid). But if no space is
available, realloc does a malloc and copies elements from the old place to
the new. Besides, you can only use malloc etc with POD types (or just
bytes).
Jul 22 '05 #5
"John Harrison" <jo*************@hotmail.com> wrote in message
"Andreas Bauer" <ba*****@mailbroy.informatik.tu-muenchen.de> wrote in
MyType* array;
= new MyType[100];

This of course works fine, but I have the need to dynamically grow the
boundaries of that array at runtime. I can't seem to use std ::
vector or any other container, cause I've got two more "lists" which
keep pointers to the elements of `array'.

The answer is very simple, don't keep pointers to the 'array'. This will
cause problem whatever means of resizing the array you use, malloc/realloc, new, vector, its all the same.

Instead keep integer indexes in your lists, these will always be valid.


A very good solution too.

But what if the array of pointers has pointers to objects from different
array's? Then keeping the index is not enough. You need a struct that
contains a pointer to the array as well as an integer index.
Jul 22 '05 #6
Andreas Bauer wrote:
I've got an array in the vein of

MyType* array;
arry = new MyType[100];

This of course works fine, but I have the need to dynamically grow the
boundaries of that array at runtime. I can't seem to use std ::
vector or any other container, cause I've got two more "lists" which
keep pointers to the elements of `array'.

So, if I used std :: vector in order to resize `array', both lists of
pointers would contain garbage, given the resize operation of the
container would shift the memory around - something which I can hardly
check statically.

So what is a good way of dynamically resizing an array without having
to rearrange the internal memory representation? Or, am I on the
totally wrong track here... (might well be). I thought of using
malloc and realloc, but then these do not call the constructors and
destructors of MyType.

You can use vector::reserve() to reserve memory (but no objects get
created) for future expansion:
#include <vector>
int main()
{
using std::vector;
vector<int>someVec;

// Reserves memory for 500 objects.
// Future expansion up to this number
// guarantees that no objects will be moved
// No objects get created
someVec.reserve(500);
// Other operations
// ...

someVec.resize(100);

// ...
}

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Bren | last post by:
Can anybody tell me what is wrong with this code? void SystemModule::AddModuleDependency(PTR_MODULE_INFO pModuleInfo) { if (!pModuleInfo) return; PTR_MODULE_INFO pDest = 0; if...
1
by: Henrik J | last post by:
Hello group... I have this little problem: I'm using a struct **foo. I have allocated x foo's using malloc: foo=(FOO**)malloc(Amount*sizeof(FOO*)); No problem....!
2
by: DrBob | last post by:
gcc 3.3 on Mac OS X. I need to dynamically grow a buffer by concatinating raw binary data in chunks. How do I use 'new' to grow the buffer size as its contents grow? I know this can be done...
18
by: Kumar | last post by:
Hi, Can somebody advise me on what is the best alternative for realloc in C++? Is it safe to "realloc"ate memory allocated using new operator ?
12
by: Michael | last post by:
How would I go about shrinking the buffer that was allocated with new, or expanding it in place? I basically need a realloc equivalent for new. Thanks in advance. Michael
86
by: Walter Roberson | last post by:
If realloc() finds it necessary to move the memory block, then does it free() the previously allocated block? The C89 standard has some reference to undefined behaviour if one realloc()'s memory...
5
by: matt.wolinsky | last post by:
Hello C++ gurus, I am trying to learn about how to use C++ memory management, and I know that there is no "renew" command in C++. What I am hoping to do is slightly different though. I want...
8
by: barcaroller | last post by:
Does C++ have an equivalent to C's realloc() that can be safely used with C++'s new and delete. If not, what is the proper way to resize memory blocks in C++ (other than using malloc/realloc/free)?
1
by: Richard Harter | last post by:
On Fri, 27 Jun 2008 09:28:56 -0700 (PDT), pereges <Broli00@gmail.comwrote: There are some obvious questions that should be asked, e.g., is the contents of your array already sorted as your...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.