xennygrimmato
xennygrimmato

Reputation: 2796

Functioning of memset

I have read in some places that memset writes "byte-wise".

Suppose I have an array, int a[100] and I use memset(a,127,sizeof(a)); , will every byte of every integer be assigned the bitset 1111111 ?

That is, will every element of the array now hold a very large integer? (2139062143 to be exact)

Upvotes: 0

Views: 594

Answers (2)

ravi
ravi

Reputation: 10723

void* memset( void* dest, int ch, std::size_t count );

Converts the value ch to unsigned char and copies it into each of the first count characters of the object pointed to by dest. If the object is not trivially-copyable (e.g., scalar, array, or a C-compatible struct), the behavior is undefined. If count is greater than the size of the object pointed to by dest, the behavior is undefined.

So, yes every byte would be assigned 111...

Upvotes: 0

timrau
timrau

Reputation: 23058

Yes, if you correct the third parameter of memset() into

memset(a, 127, sizeof(a));

Upvotes: 3

Related Questions