Skip to main content
GameDev.net gamedev.net
🔒 Locked

Fun with MEMSET

Started by Medgur Oct 23, 2000 at 9:07 PM 10 replies 650+ views
Original Post
Medgur
Medgur
Ok, I''ve been having a little fun with this great function, mainly I keep resetting my computer. So, any help would be much appreciated. This is what I''m doing: I''ve got an array of doubles of length screen_width*screen_height and I would like to set all members of the array to zero. Currently this is what I''m doing: memset(&array, 0, screen_width*screen_height); BTW - is there a faster way then just a memset? Thanks, -Medgur
farmersckn
farmersckn
doesn''t memset just take a pointer? i think you do:
double * array[screen_width * screen_height];
memset (array, 0, screen_width * screen_height);

also, i think you must use sizeof (x) because the third parameter is the number of *bytes* to copy. (i think).
so do this:
memset (array, 0, sizeof (double) * screen_width * screen_height);
Yesterday is the past, tomorrow is the future. Today is a gift, that is why we call it the present.
Liquid
Liquid
Your memset call should read like:

memset( array,0,sizeof(double)*(screen_width*screen_height) );

don''t use &array because memset expects a pointer, and array is already a pointer. You would only use the address-of operator if you were passing in a starting position in the array, for example, memset( &array[15],0,... ). And the last parameter, the size, is in bytes. If you have a 640*480 screen, your call would be telling memset to zero 307200 bytes, which is not correct since a double is usually 4 bytes, and 4*640*480 is 1228800 bytes which is the real size of your array. Hope that makes sense.

Liquid
Liquid
Liquid
Also, your array should just be declared

double array[screen_width*screen_height];

if it isn''t already.

Liquid
Medgur
Medgur
This is what I have now:
memset(array,0,sizeof(double) * screen_width * screen_height);

Which is what I had about 2 hours ago, before I developed that form I asked the question with.

It still resets the computer.

I performed a complete recompile as well, just to be that much more sure.

As for the array, it''s initially defined as:
double *array;
Then initialized later in the code as:
array = new double[screen_width * screen_height];

Not that it makes any difference, but I thought it might be useful to know.

Thanks,
-Medgur
Gladiator
Gladiator
I''m sure that if you check whether or not the "array" pointer is NULL, it will return TRUE.

Try it:

if ( array == NULL )
{
MessageBox( 0, "OPPSS", "Error", 0 );
}

If that''s not the case, which I doubt, please post your entire code so we could take a look at it.

Hope this helps!

-------------------------------
I'll screw up whoever screws around with the gamedev forum!

..-=gLaDiAtOr=-..
Medgur
Medgur
Ok, the pointer is null.
Now it all seems to make sense.

Of course - I still have no idea how to fix it.

To my limited knowledge it doesn''t seem right that the pointer that is array is null. Hrm...

Thanks,
-Medgur
SiCrane
SiCrane
When a call to new returns a null pointer it means that there wasn''t enough memory to perform the allocation.

Also a call to memset may be improved depending on your compiler. For example Borland C++ will emit the function bodies of certain functions inline with your code. These functions are called intrinsic functions in Borland C++, and I believe includes memset.
Cygon
Cygon
Perhaps 640 x 480 (or more) doubles are too much. But the pointer just being NULL... I thought new would throw an exception if the allocation failed ?

Whatever, you can try to handle the array dynamically, this will also deliver a speedup because if the embedded row-offsets:
    
double **g_ppScreen = NULL;
int g_nScreenWidth = 0;
int g_nScreenHeight = 0;

int AllocScreenmem(int p_nWidth, int p_nHeight) {
int l_nY;

FreeScreenmem();

g_nScreenWidth = p_nWidth;
g_nScreenHeight = p_nHeight;

// Reserve an array for the lines

g_ppScreen = (double **)malloc(p_nHeight * sizeof(*g_ppScreen));
if(!g_ppScreen) {
fprintf(stderr, "AllocScreenmem() - Out of memory!\n");
return 0;
}

for(l_nY = 0; l_nY < p_nHeight; l_nY++)
g_ppScreen[l_nY] = NULL;

for(l_nY = 0; l_nY < p_nHeight; l_nY++) {
g_ppScreen[l_nY] = (double *)malloc(p_nWidth * sizeof(**g_ppScreen));
if(!g_ppScreen[l_nY]) {
fprintf(stderr, "AllocScreenmem() - Out of memory!\n");
FreeScreenmem();
return 0;
}
}

return;
}

void FreeScreenmem(void) {
int l_nY;

if(g_ppScreen) {
for(l_nY = g_nScreenHeight - 1; l_nY >= 0; l_nY--)
if(g_ppScreen[l_nY])
free((void *)g_ppScreen[l_nY]);

free((void *)g_ppScreen);
g_ppScreen = NULL;
}

return;
}


Then, all you need to access the array is to write

double value = g_ppScreen[Y][X];
g_ppScreen[Y][X] = 0.0;

You could also do this in C++, overload the () operator in an inline function which does bound checking in debug mode!

-Markus-
P.S. I have just written this code in Netscape, better double-check it
Professional C++ and .NET developer trying to break into indie game development.
Follow my progress: http://blog.nuclex-games.com/ or Twitter - Topics: Ogre3D, Blender, game architecture tips & code snippets.
SiCrane
SiCrane
when using STL, new will throw an exception if the allocation fails, (and even then only if you included the header files correctly) if not using STL, new only signals failure by returning a NULL pointer, unless _set_new_handler() is called to set a handler to manage memory allocation failures. Even then you still have to check the result of the new allocation for a NULL value because the new handler can still fail.

Note that _set_new_handler() is distinct from the STL set_new_handler(), which calls the new handler and disables the exception that would be thrown by the STL version of new.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.