   What's wrong with my ZeroMemory function?
  Allo,My program is somtimes crashing when I use my ZeroMemory function... do you know why?typedef OXDWORD unsigned long;typedef OXQWORD unsigned __int64;void OXZeroMemory (void *pDest, const OXDWORD iSize){OXDWORD qwordCount = iSize >> 3;((OXQWORD*)pDest)[qwordCount] &= (0xFFFFFFFFFFFFFFFF << ((iSize % 8) << 3));while (qwordCount--) ((OXQWORD*)pDest)[qwordCount] = 0;}Thank you,Gab  
  What''s wrong with using memset()?Writting to memory that doesn''t belong to you is not very nice.;  I was making an effort to optimize a little... I know that I''m writting in memory that is outside the range but I''m not changing the value of that memory... Is that wrong?  ;  Yes, that is wrong. You may cross page boundaries and the CPU will trigger a page fault at that point.Also, memset() is probably far better optimized than your version. Did you benchmark the your function? How much faster is it compared to memset()?If memset() is slowing you down, then you should probably look into calling it less oftern instead of trying to speed it up with your own (slower and incorrect) version.;  Technically it is - you have to be able to guarantee alignment to have it function correctly (and alignment is implementation dependent).I would expect that code to be a de-optimization from the memset provided by MSVC6/7, btw.You know, you''d think there''d be an op code to clear a chunk of memory.  ;  There is: rep stosbHaving it do it fast is another problem entirely.;  I did not understand the last reply but I did understand the other ones... Thank you every one I will use memset in the future... I guess I tryed to do too much...   ;  rep stosb is an assembly instruction to fill a block of memory with a given value, roughtly equivalent to memset. memset actually uses rep stosd, which fills by dwords, and manually fills byte leftovers on either sides.edit: with #pragma intrinsic or the respective compiler option, msvc will generate inline code for memset. if you have a 32-bit-aligned piece of memory whose size is also divisible by 32 bits, such as an integer array, memset(dest, 0, count) will be expanded to something likelea di, [dest]xor eax, eaxmov ecx, countrep stosdmaking it impossible to beat.edit: in flat memory model, we don't reload segments?[edited by - niyaw on November 5, 2002 12:44:12 AM]