Skip to main content
GameDev.net gamedev.net
🔒 Locked

Copy an array?

Started by Amarth Apr 21, 2005 at 12:04 PM 5 replies 1.3k views
Original Post
Amarth
Amarth
I have two arrays, say

Foo* aFooArray;
Foo* anotherFooArray;

aFooArray = new Foo[50];
anotherFooArray = new Foo[50];

I want to move aFooArray to anotherFooArray... I want to make new elements in aFooArray and put the old ones in anotherFooArray, and simply forget the old ones at anotherFooArray. The question is, how to do this fast and without leaking memory? The sure way is to copy every element, but it's slow and, I think, not necessary. The other way is to copy the pointer aFooArray to anotherFooArray, but I'm pretty sure this asking for trouble. I can see a way involving lots of 'new's and 'delete's, but I suppose that's slow as hell... So what's the correct way of doing this? Or should I chance something else? Use std::vector? Or don't use new[]? Other ideas? Also, does this chance for multi-dimensional arrays? Thanks, -Amarth
deadimp
deadimp
I dunno what you're asking exactly... You mean moving the stuff from 'aFooArray' to 'anotherFooArray', and creating a new array for 'aFooArray'? If so:
delete [] anotherFooArray; //Remove all of the elements hereanotherFooArray=aFooArray;aFooArray=new Foo[50];

And yeah, I think it might work with multi-dimensional arrays. These types of arrays don't affect the way the arrays are stored in memory, merely how they are read and manipulated in coding.
Projects:> Thacmus - CMS (PHP 5, MySQL)Paused:> dgi> MegaMan X Crossfire
Trap
Trap
Depending on what Foo is/means you can either use the code deadimp posted or do:
std::copy(aFooArray,aFooArray+50,anotherFooArray);

What is appropriate depends on what kind of semantics copying, deleting and creating a Foo has in your program.
graveyard filla
graveyard filla
If you dont want to copy each element 1 by 1, then post more code and try to explain better on what you're trying to do, since usually you can get around having to copy entire arrays like that.
FTA, my 2D futuristic action MMORPG
Amarth
Amarth
In my particular program, the Foo is a GridElement, as in

struct GridElement{	bool filled;	int color;};GridElement** current;GridElement** next;current = new GridElement*[MAX_PIECE_SIZE];next = new GridElement*[MAX_PIECE_SIZE];for(i=0;i<MAX_PIECE_SIZE;i++){	current = new GridElement[MAX_PIECE_SIZE];	next = new GridElement[MAX_PIECE_SIZE];}


It's a falling-block, tetris-like game :). Anyway, the next array needs to be copied to the current array, every time a block is a the bottom. Then the next array needs to be filled with new data.

Perhaps I don't even need to use new here, not sure. Just fire away, I'm happy to learn :).

I'd like to know what would be a standard approach to this sort of problems... Whenever somthing like that exists :). Because I might be doing it with complex classes too... But for now, just this question :).

Thanks,
-Amarth
Tera_Dragon
Tera_Dragon
I'm not sure what you want, but You can use memcpy to copy data.
Why not use
GridElement current[MAX_PIECE_SIZE * MAX_PIECE_SIZE];

It should make data copying easier. Also it doesn't look like you need to be using new. You should only use new when at compile time you don't know how many elements in the array you want.
____________________________________________________________Programmers Resource Central
deadimp
deadimp
Yeah, I know this is a somewhat old post, but I must conject (inject, whatever): You actually use pointers (which can use the new operator) with dynamic arrays AND polymorphism.
Projects:> Thacmus - CMS (PHP 5, MySQL)Paused:> dgi> MegaMan X Crossfire

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.