ethico's blog

By ethico, history, 4 years ago, In English

Hey fellows,

-->i want to know about memset,how its work,because when i put 0 or -1 as like as memset(a,-1,a.sizeof(a)) or memset(a,0,a.sizeof(a)),i am getting values,but if i put any other random number,why it is showing garbage values?

--> moreover,can u guys suggest me online best website for learning **cpp(c++).

  • Vote: I like it
  • -9
  • Vote: I do not like it

| Write comment?
»
4 years ago, # |
Rev. 3   Vote: I like it 0 Vote: I do not like it

That is because memset sets values byte by byte. This will not be a problem for 0 or -1, because all the bit values are equal in 0 or -1. However, if you choose any other integer like 5, for example, we will get the following result:

00000101 00000101 00000101 00000101 (84215045)

Memset will set each of the 4 bytes of the integer to 5, and this will result in a completely different integer value.

Although, you could have just googled this.

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Look at this link :- Link