Results 1 to 19 of 19

Thread: [resolved] ZeroMemory, for() loop, struct

  1. #1

    Thread Starter
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802

    [resolved] ZeroMemory, for() loop, struct

    Why won't this work?

    Code:
    typedef struct _GHOST
    {
    	int width;
    	int height;
    	int x;
    	int y;
    	int arrayx;
    	int arrayy;
    	int drctn;
    	int speed;
    }GHOST;
    
    GHOST g_Ghost[3];
    
    for(int i = 0; i < 3; i++);
    	{
    		ZeroMemory(&g_Ghost[i], sizeof(g_Ghost[i]));
    		g_Ghost[i].width = 20;
    		g_Ghost[i].height = 20;
    		g_Ghost[i].arrayx = 18;
    		g_Ghost[i].arrayy = 1;
    		g_Ghost[i].x = 360;
    		g_Ghost[i].y = 20;
    		g_Ghost[i].drctn = 1;
    		g_Ghost[i].speed = 4;
    	}
    It works if I replace the i with a 1.

    Then I have 165 lines of code moving this ghost around the screen (mostly collision detection). All the code will be the same for all my ghost (I think I will have four ghosts), should I put all the 165 lines of code in one big for() loop or should I copy and paste the code four times. As this code is inside a game-loop it needs to be as fast as possible.
    Last edited by McCain; Dec 11th, 2002 at 04:00 PM.
    Never argue with fools, they will only drag you down to their level, and beat you with experience.

    Q: How do you tell an experienced hacker from a novice?
    A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Code:
    ZeroMemory(&g_Ghost[i], sizeof(g_Ghost[i]));
    Replace this with:
    Code:
    ZeroMemory(&g_Ghost[i], sizeof(GHOST));
    The size of each structure is the same.

    It will be faster to put it separately, but any good compiler should unroll the loop if it thinks it will make any difference. I doubt it will, since the loop overhead with 165 lines is pretty small, really.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3

    Thread Starter
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802
    Why doesn't it work when I have sizeof(g_Ghost[i]) ?
    Does vc6 == "any good compiler"?
    Never argue with fools, they will only drag you down to their level, and beat you with experience.

    Q: How do you tell an experienced hacker from a novice?
    A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    VC6 will unroll loops if it deems it necessary, yes.

    I'm not totally sure, but I believe it doesn't work because 'i' can only be evaluated at runtime, yet sizeof() is always evaluated at compile time, and as such requires a constant expression.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    That wouldn't matter parksie, g_Ghost[x] is of type GHOST no matter what is passed.

    Is this real C code (it looks like it)? If so, then you have to declare all variables at the start of the block, you may not declare them inside a for statement:
    Code:
    void InitGhosts()
    {
    	int i;
    	for(i = 0; i < 3; i++);
    	{
    		ZeroMemory(&g_Ghost[i], sizeof(g_Ghost[i]));
    		g_Ghost[i].width = 20;
    		g_Ghost[i].height = 20;
    		g_Ghost[i].arrayx = 18;
    		g_Ghost[i].arrayy = 1;
    		g_Ghost[i].x = 360;
    		g_Ghost[i].y = 20;
    		g_Ghost[i].drctn = 1;
    		g_Ghost[i].speed = 4;
    	}
    }
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  6. #6

    Thread Starter
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802
    Ok, I think I'll go with a for loop for my 165 lines of code then...
    CornedBee: I'm compiling this as c++ code so the declaration of a variable inside a for loop shouldn't be a problem. Why did you think it was pure c code? Have I done anything "the c way" that there is a better way of doing in c++?
    Never argue with fools, they will only drag you down to their level, and beat you with experience.

    Q: How do you tell an experienced hacker from a novice?
    A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    typedef struct _GHOST {
    ...
    } GHOST;

    That's something not needed in C++. You can write

    struct GHOST {
    ...
    };

    and still declare variables as easy as
    GHOST var;
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  8. #8

    Thread Starter
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802
    Ok, thanks that explains why I didn't recognize that code even though I've read a few c++ tutorials on structs...

    And now I know what was wrong with my code... I can't believe I did such a silly mistake... Look at my code... Do you see that little semicolon at the end of the for() line? Well, that's the problem right there... i never got a value... Silly me
    Never argue with fools, they will only drag you down to their level, and beat you with experience.

    Q: How do you tell an experienced hacker from a novice?
    A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer

  9. #9
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    Why not just use "memset(g_Ghost, 0, sizeof(GHOST)*3);" before running the loop?

    Z.

  10. #10

    Thread Starter
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802
    Why is that better?
    Never argue with fools, they will only drag you down to their level, and beat you with experience.

    Q: How do you tell an experienced hacker from a novice?
    A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer

  11. #11
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    ZeroMemory and memset(., 0, .) have the same effect.

    But Zaei's method is one function call instead of three, speeding it up a little, especially if you add more ghosts. It's simply more efficient to zero out one large chunk instead of three small chunks.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  12. #12
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    Originally posted by CornedBee
    ZeroMemory and memset(., 0, .) have the same effect.

    But Zaei's method is one function call instead of three, speeding it up a little, especially if you add more ghosts. It's simply more efficient to zero out one large chunk instead of three small chunks.
    ZeroMemory() is a macro to RtlZeroMemory(), which is a macro to memset(..., 0, ...). Using memset also makes your code portable.

    Z.

  13. #13
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Originally posted by Zaei
    ZeroMemory() is a macro to RtlZeroMemory(), which is a macro to memset(..., 0, ...). Using memset also makes your code portable.

    Z.
    Indeed. Didn't know that...

    Turns out it is a macro reference to memset except on 64-bit CPUs, where it's a function from ntdll.dll.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  14. #14
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    Originally posted by CornedBee
    Turns out it is a macro reference to memset except on 64-bit CPUs, where it's a function from ntdll.dll.
    That would be the reason to use the macro... Though I think with a 64 bit compiler, the memset function would simply be changed to accept 64 bit pointers.

    Z.

  15. #15
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    It might be faster to use the call from ntdll, perhaps (taking advantage of specific IA-64 things you can do as the OS?)
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  16. #16
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    Originally posted by parksie
    It might be faster to use the call from ntdll, perhaps (taking advantage of specific IA-64 things you can do as the OS?)
    Its quite possible.... *shrug*

    By the way, parksie...



    Z.

  17. #17
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    That should actually be about 20 minutes. One moment...
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  18. #18
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    I rebooted it a couple of days ago (which was a good test anyway), and I cleaned it all this afternoon.

    *Thoroughly*

    Things like the PSU, heatsink, etc., were not spared

    Will be down for a lot longer tomorrow (woo, reinstall ).
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  19. #19

    Thread Starter
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802
    Thanks for your help guys, I'm now useing memset and I don't have that semicolon, so now it works perfect, thank you!
    Never argue with fools, they will only drag you down to their level, and beat you with experience.

    Q: How do you tell an experienced hacker from a novice?
    A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width