|
-
Dec 8th, 2002, 04:55 PM
#1
Thread Starter
Fanatic Member
[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
-
Dec 8th, 2002, 05:07 PM
#2
Monday Morning Lunatic
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
-
Dec 9th, 2002, 03:06 AM
#3
Thread Starter
Fanatic Member
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
-
Dec 9th, 2002, 04:15 AM
#4
Monday Morning Lunatic
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
-
Dec 9th, 2002, 08:09 AM
#5
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.
-
Dec 9th, 2002, 09:48 AM
#6
Thread Starter
Fanatic Member
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
-
Dec 9th, 2002, 10:41 AM
#7
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.
-
Dec 9th, 2002, 02:08 PM
#8
Thread Starter
Fanatic Member
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
-
Dec 9th, 2002, 03:11 PM
#9
Frenzied Member
Why not just use "memset(g_Ghost, 0, sizeof(GHOST)*3);" before running the loop?
Z.
-
Dec 10th, 2002, 03:46 AM
#10
Thread Starter
Fanatic Member
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
-
Dec 10th, 2002, 06:31 AM
#11
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.
-
Dec 10th, 2002, 08:04 AM
#12
Frenzied Member
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.
-
Dec 10th, 2002, 09:28 AM
#13
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.
-
Dec 10th, 2002, 10:14 AM
#14
Frenzied Member
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.
-
Dec 10th, 2002, 11:40 AM
#15
Monday Morning Lunatic
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
-
Dec 10th, 2002, 11:58 AM
#16
Frenzied Member
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.
-
Dec 10th, 2002, 12:09 PM
#17
Monday Morning Lunatic
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
-
Dec 10th, 2002, 12:10 PM
#18
Monday Morning Lunatic
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
-
Dec 11th, 2002, 03:59 PM
#19
Thread Starter
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|