Can anybody tell me the use of memset over simple loop initialization
or int a[10]={0} . This also initializes a[0] to a[9] with 0..
So whats the use of using memset .Is it faster?
Your example only works once (during creation) and only for stack arrays. Additionally, you can't set the other elements to some other value, only zero (for example, you can't set all elements to 6).
memset() can be used at any time and for any kind of memory structure, including single variables, arrays, and objects. The limitation is that it can't properly set each element in an array to any value, because it doesn't have the concept of element. It initializes memory, not arrays. If you want to set all the elements in an array of chars to 6, that's doable, because each element uses exactly one byte, but you can't do the same with an array of ints. On the other hand, you could set all the elements to 0x06060606 (assuming sizeof(int)==4).
std::fill() does have the concept of elements, and allows you to set each element to a value (e.g. std::fill(a,a+10,6);). It's usually slower than memset(), though, but it's also usually the correct choice.