Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

initializing array with ZeroMemory

char cMsg[128][12];

is the same this:

ZeroMemory(cMsg, sizeof(cMsg));

than this?

for(i=0;i<128;i++)
    ZeroMemory(cMsh[i], sizeof(cMsg[i]))

compilers gives no error with both ways, but are they metods for same objective?

like image 474
Martin Lopez Avatar asked Jun 10 '26 09:06

Martin Lopez


2 Answers

The behavior is going to be the same for char arrays, because they have no alignment requirements or padding bytes in the middle. The first way of doing it may be a little faster than the second one, because it uses fewer function calls, the speed-up would be too small to measure reliably on modern hardware, and the net result is going to be the same.

like image 150
Sergey Kalinichenko Avatar answered Jun 11 '26 23:06

Sergey Kalinichenko


Yes.

ZeroMemory zeroes a block of memory, and arrays are contiguous.

So, zeroing out the entire block in "chunks" rather than all in one go is functionally the same.

like image 29
Lightness Races in Orbit Avatar answered Jun 12 '26 00:06

Lightness Races in Orbit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!