Skip to main content
gamedev.net
🔒 Locked

Default variable assignment

Started by Leadorn Jun 13, 2002 at 4:23 AM 6 replies 400+ views
Original Post
Leadorn
Leadorn
Hail I have done some writing to file test, and I have discovered that my unsigned char variable on creation gets assigned with 0xdc. Why wasn''t my unsigned variable assigned 0x00? Is there an technical explanation for this, or is it the compiler?
Grul
Grul
Unintialized variables or pointers always get the value 0xCDCDCD... etc in Windows. That is why you should always initialize your variables before you use them. Initialize them to 0 if you don''t know which value it is going to have at the moment.
"Archangel, dark angellend me thy lightthrough death''s fail until we have heaven in sight"
Leadorn
Leadorn
Why did they choose 0xdc?



I use this code to create a variable to 500 byte array, how do I assign them all to NULL the easiest way?


    
unsigned char *he = new unsigned char [500];


[edited by - leadorn on June 13, 2002 5:55:03 AM]
felonius
felonius
use the standard library function memset().
Jacob Marner, M.Sc.Console Programmer, Deadline Games
Leadorn
Leadorn
mkay

That make sense, using it as debug tool.
DarkEmpire
DarkEmpire
Instead of using memset, you can use your own initialisation function:

http://www.hailstorm.net/papers/smallwin32.htm#buffer

here is the content for ZeroMemory:



#ifdef FillMemory
#undef FillMemory
#endif
#ifdef ZeroMemory
#undef ZeroMemory
#endif



__forceinline
void FillMemory(void *dest,unsigned __int8 c,unsigned __int32 count)
{
unsigned __int32 size32=count>>2;
unsigned __int32 fill=(c<<24|c<<16|c<<8|c);
unsigned __int32 *dest32=(unsigned __int32*)dest;

switch( (count-(size32<<2)) )
{
case 3: ((unsigned __int8*)dest)[count - 3] = c;
case 2: ((unsigned __int8*)dest)[count - 2] = c;
case 1: ((unsigned __int8*)dest)[count - 1] = c;
}

while( size32-- > 0 )
*(dest32++) = fill;

} /* FillMemory */

#define ZeroMemory(dest,count) FillMemory(dest,0,count)


Leadorn
Leadorn
Mkay thanks guys and girls.

Topic Locked

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

Sign in to reply to this topic.