|
18 Dec 2002, 08:43
|
#1
|
Rawr rawr
Join Date: Dec 2000
Location: Upside down
Posts: 5,300
|
C/C++: fastest array copy
What is the fastest way to copy an array in C/C++?
I have to copy an 2D array of an unknown size (second dimension is known) to another 2D array.
I'm looking for the fastest possible copy.
BTW, how would I write a copy constructor in C++? Is it simply overriding the '=' operator?
TY
|
|
|
18 Dec 2002, 10:20
|
#2
|
Xenoc
Join Date: Feb 2001
Location: Great Britain
Posts: 297
|
Re: C/C++: fastest array copy
Quote:
Originally posted by Structural Integrity
What is the fastest way to copy an array in C/C++?
I have to copy an 2D array of an unknown size (second dimension is known) to another 2D array.
I'm looking for the fastest possible copy.
|
I guess you are saying the size is not known at compile time, as you will need to know it at runtime to know when to stop copying data...
A simple memcpy() should suffice.
There might be compiler pragmas that affect how much bounds checking the runtime code does - but the added overhead would have negligible impact for larger copy statements.
Quote:
BTW, how would I write a copy constructor in C++? Is it simply overriding the '=' operator?
|
Yes, you just overload the operator=() method for the class.
|
|
|
18 Dec 2002, 18:41
|
#3
|
/dev/zero Retired Mod
Join Date: May 2000
Posts: 415
|
im not the worlds greatest c programmer but would something along these lines work ..
Code:
int **my2darray;
int **myotherarray;
my2darray = malloc(...);
myotherarray = malloc(sizeof(my2darray));
memcpy(myotherarray,my2darray,sizeof(my2darray));
?
__________________
#linux : Home of Genius
<idimmu> ok i was chained to a desk with this oriental dude
|
|
|
18 Dec 2002, 18:49
|
#4
|
Rawr rawr
Join Date: Dec 2000
Location: Upside down
Posts: 5,300
|
can "malloc" be replace with "new"? (while keeping the same speed)
ATM I have this:
vertexA = new float[numvertices][3];
memcpy(vertexA, obj->vertexA, SizeOf(float[3])*numvertices);
|
|
|
18 Dec 2002, 19:47
|
#5
|
Bitch
Join Date: Jun 2002
Location: North Yorkshire
Posts: 3,848
|
Quote:
Originally posted by Structural Integrity
can "malloc" be replace with "new"? (while keeping the same speed)
|
Not knowing much about C++ this is purely an educated guess but I'd say almost definitely not as new and delete provide much greater functionality...
__________________
ACHTUNG!!!
Das machine is nicht fur gefingerpoken und mittengrabben. Ist easy
schnappen der springenwerk, blowenfusen und corkenpoppen mit
spitzensparken. Ist nicht fur gewerken by das dummkopfen. Das
rubbernecken sightseeren keepen hands in das pockets. Relaxen und vatch
das blinkenlights!!!
|
|
|
18 Dec 2002, 20:37
|
#6
|
Xenoc
Join Date: Feb 2001
Location: Great Britain
Posts: 297
|
Quote:
Originally posted by Structural Integrity
can "malloc" be replace with "new"? (while keeping the same speed)
|
As the link posted by Gayle28uk says, for built-in types, new and malloc are functionally equivalent, this is because there are no constructors for built-in types.
The assembler the compiler generates for malloc or new for an array of float is likely to be identical (or at least perform equally).
There is a slight difference in free and delete, in that delete is guaranteed to handle a null pointer gracefully, whereas free will probably crash.
I think I would use
Code:
vertexA = new float[numvertices][3];
memcpy(vertexA, obj->vertexA, SizeOf(float)*3*numvertices);
even if that is potentially an extra multiplication operation at runtime (depending on how clever the optimizer is).
|
|
|
18 Dec 2002, 21:05
|
#7
|
/dev/zero Retired Mod
Join Date: May 2000
Posts: 415
|
Quote:
Originally posted by Structural Integrity
can "malloc" be replace with "new"? (while keeping the same speed)
|
I wish you wouldnt say C/C++ when you actually mean C++ :/
__________________
#linux : Home of Genius
<idimmu> ok i was chained to a desk with this oriental dude
|
|
|
18 Dec 2002, 22:34
|
#8
|
Rawr rawr
Join Date: Dec 2000
Location: Upside down
Posts: 5,300
|
Quote:
Originally posted by MT
I wish you wouldnt say C/C++ when you actually mean C++ :/
|
Oi.... sowwy MT... I won't do it again, it's just that C and C++ are so alike.
|
|
|
19 Dec 2002, 03:54
|
#9
|
Ball
Join Date: Oct 2001
Posts: 4,410
|
Quote:
Originally posted by zenopus
There is a slight difference in free and delete, in that delete is guaranteed to handle a null pointer gracefully, whereas free will probably crash.
|
No.
Code:
free() frees the memory space pointed to by ptr, which
must have been returned by a previous call to malloc(),
calloc() or realloc(). Otherwise, or if free(ptr) has
already been called before, undefined behaviour occurs.
If ptr is NULL, no operation is performed.
|
|
|
19 Dec 2002, 04:00
|
#10
|
Ball
Join Date: Oct 2001
Posts: 4,410
|
Quote:
Originally posted by zenopus
I think I would use
Code:
vertexA = new float[numvertices][3];
memcpy(vertexA, obj->vertexA, SizeOf(float)*3*numvertices);
even if that is potentially an extra multiplication operation at runtime (depending on how clever the optimizer is).
|
Any particular reason? Or just aethetics?
Structural, you've got it. Just remember to use delete [] vertexA instead of delete vertexA.
How is your engine doing?
|
|
|
19 Dec 2002, 08:26
|
#11
|
Rawr rawr
Join Date: Dec 2000
Location: Upside down
Posts: 5,300
|
Quote:
Originally posted by queball
Any particular reason? Or just aethetics?
Structural, you've got it. Just remember to use delete [] vertexA instead of delete vertexA.
How is your engine doing?
|
I guess deleting it like that also works that way for 2D arrays? I guess so since any other syntax generates an error. Just want to be sure
From my experience delete causes a runtime error when performed on an already deleted object. Quite nasty since It's hard to trace and even nastier when you have dynamic arrays since the pointer is copied, not the array itself. Simply calling delete in the destructor is a big nono because you remove data for still existing objects. I think it also fails when performed on a null pointer, although I'm not fully sure.
I certainly check for a null pointer before even calling delete.
My engine does quite well... I have a great deal of objects that handle the maths, I have finally defined how data is stored for models and am now working on collision detection at triangle level (the very primitive of a model).
Problem is that the sheer amount of maths is slowing it down a "tad" since I have a lot of catching up to do, I don't want to implement something I don't understand. The funny thing is that I'm slowly developing a fascination for maths now, the very subject I have despised for many years.
Don't expect any fancy things like Doom32 of Quake76 from me in the near future as I'm still learning a lot :smiley1:
__________________
"Yay"
|
|
|
19 Dec 2002, 12:06
|
#12
|
Tourist
Join Date: Jun 2001
Location: moon
Posts: 90
|
Quote:
Originally posted by MT
im not the worlds greatest c programmer but would something along these lines work ..
Code:
int **my2darray;
int **myotherarray;
my2darray = malloc(...);
myotherarray = malloc(sizeof(my2darray));
memcpy(myotherarray,my2darray,sizeof(my2darray));
?
|
no, because:
Quote:
The sizeof operator cannot return the size of dynamically allocated arrays or external arrays.
|
I fear you have to keep track of the original array dimension and do something like:
Code:
int **MyArray; // original
int **MyCopyArray; // the copy
int d1, d2; // containing the dimension of the original array
int nxd1, nxd2; // counters
// Allocate memory for the array
MyCopyArray = new int*[d1];
for (ndx1 = 0; ndx1 < d1; ndx1++) {
MyCopyArray[ndx1] = new int[d2];
for (ndx2 = 0; ndx2 < d2; ndx2++) // Copy the values
MyCopyArray[ndx1][ndx2] = MyArray[ndx1][ndx2];
} // for (ndx1 = 0; ndx1 < d1; ndx1++)
I just though that
Code:
MyCopyArray[ndx1] = memcpy( MyCopyArray[ndx1], MyArray[ndx1], sizeof(int)*d2 );
instead of the second for-loop should do it, too.
Deleting has to be done the following way:
Code:
// Delete the array
for (ndx1 = 0; ndx1 < d1; ndx1++)
delete [] MyArray[ndx1];
delete [] MyArray;
__________________
Quote:
Originally posted by Bloomers III
sex is dirty and for losers who can't masturbate properly
|
|
|
|
|
All times are GMT +1. The time now is 03:54.
| |