Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

ThomW

Copying BOOL arrays

This topic is 6912 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
This SHOULD work:

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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
How about this?

struct SKeyInfo{
    BOOL Keys[ 256 ];
};

    SKeyInfo kiCurrent, kiLast;
    kiLast = kiCurrent;

Kwanji

Share this post


Link to post
Share on other sites
No, Anon, arrays are just pointers to the first element. Saying:

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( );

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
*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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!