Martin Lopez
Martin Lopez

Reputation: 33

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?

Upvotes: 3

Views: 1196

Answers (2)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726849

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.

Upvotes: 5

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385284

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.

Upvotes: 4

Related Questions