Copying BOOL arrays
I have a really stupid question.
I have a program in VC++ 6.0 with two BOOL arrays:
keys[256] and lastkeys[256]
What''s the quickest way to copy the contentes of keys into lastkeys? Right now I''m doing it with a for loop. I''ve tried memcopy, but I can''t seem to get that to work...
Thanks in advance!
ThomW
ThomWwww.LMNOpc.com
This SHOULD work:
I hope that helps you out. Otherwise, a for loop will probably serve your purposes just fine without being TOO much slower.
memcpy(lastkeys, keys, sizeof(BOOL) * 256)
I hope that helps you out. Otherwise, a for loop will probably serve your purposes just fine without being TOO much slower.
May 18, 2000 09:36 PM
How about this?
struct SKeyInfo{
 BOOL Keys[ 256 ];
};
 SKeyInfo kiCurrent, kiLast;
 kiLast = kiCurrent;
Kwanji
struct SKeyInfo{
 BOOL Keys[ 256 ];
};
 SKeyInfo kiCurrent, kiLast;
 kiLast = kiCurrent;
Kwanji
No, Anon, arrays are just pointers to the first element. Saying:
...simply has kiLast.Keys point to the same array that kiCurrent.Keys points to. It doesn''t copy the array.
The fastest way to use memcpy, like jaxson said. Well, technically, the fastest way is with a memcpy32 function (copies dwords instead of bytes), since BOOL is a typedef for UINT. But you''d have to write it yourself, in Assembly.
~CGameProgrammer( );
SKeyInfo kiLast = kiCurrent;
...simply has kiLast.Keys point to the same array that kiCurrent.Keys points to. It doesn''t copy the array.
The fastest way to use memcpy, like jaxson said. Well, technically, the fastest way is with a memcpy32 function (copies dwords instead of bytes), since BOOL is a typedef for UINT. But you''d have to write it yourself, in Assembly.
~CGameProgrammer( );
~CGameProgrammer( );
Developer Image Exchange -- New Features: Upload screenshots of your games (size is unlimited) and upload the game itself (up to 10MB). Free. No registration needed.
Actually, if you do:
struct KeysInfo
{
BOOL Keys[256];
} keys, lastkeys;
lastkeys = keys;
...the compiler will handle copying the entire struct (default copy constructor is a bitwise copy).
If you have two pointers, do this:
KeysInfo *lastkeys, *keys;
// dynamically create structs here
*lastkeys = *keys;
...in most cases your compiler will come up with the most efficient way of copying the structs.
struct KeysInfo
{
BOOL Keys[256];
} keys, lastkeys;
lastkeys = keys;
...the compiler will handle copying the entire struct (default copy constructor is a bitwise copy).
If you have two pointers, do this:
KeysInfo *lastkeys, *keys;
// dynamically create structs here
*lastkeys = *keys;
...in most cases your compiler will come up with the most efficient way of copying the structs.
*zoinks*
I didn't realize that this thread would blossom like this! I used the memcpy suggestion, and it works great!
I tried memcpy on my own before posting my original question, but I wasn't specifying the size of what was being copied. I was using:
(sizeof(keys) * sizeof(keys[0]))
as the third parameter, which looked a lot like one of the examples in the VC++ help file, but (of course) that was wrong.
Thanks everybody!
ThomW
Edited by - ThomW on May 19, 2000 12:40:44 AM
I didn't realize that this thread would blossom like this! I used the memcpy suggestion, and it works great!
I tried memcpy on my own before posting my original question, but I wasn't specifying the size of what was being copied. I was using:
(sizeof(keys) * sizeof(keys[0]))
as the third parameter, which looked a lot like one of the examples in the VC++ help file, but (of course) that was wrong.
Thanks everybody!
ThomW
Edited by - ThomW on May 19, 2000 12:40:44 AM
ThomWwww.LMNOpc.com
May 20, 2000 12:10 AM
if you want things to be really fast, you could skip the whole copying thing and just swap pointers. like this
BOOL keys1[256];
BOOL keys2[256];
BOOL* keys = keys1;
BOOL* lastkeys = keys2;
BOOL* tempkeys = NULL;
while (whatever) {
// do your processing
// swap keys and lastkeys
tempkeys = lastkeys;
lastkeys = keys;
keys = tempkeys;
}
Mike
BOOL keys1[256];
BOOL keys2[256];
BOOL* keys = keys1;
BOOL* lastkeys = keys2;
BOOL* tempkeys = NULL;
while (whatever) {
// do your processing
// swap keys and lastkeys
tempkeys = lastkeys;
lastkeys = keys;
keys = tempkeys;
}
Mike
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement