The difference between memset and _strnset

I cannot understand what exactly is the difference between the two following implementations:

char str[20] = "Hello World"; _strnset(str, '*', 5); 

and

 char str[20] = "Hello World"; memset(str, '*', 5); 

They both give the following result:

Exit: ***** World!

Is there a preference between them?

+6
source share
1 answer

_strnset knows that it works with a string, so it will respect the null terminator. there is no memset , therefore it will not.

Regarding preference,

  • memset is in both C and C ++ standards, _strnset missing.
  • if available, _strnset can save you from buffer overflows if you write buggy code.

If you know you are _strnset on Windows, use _strnset . Else memset .

+11
source

All Articles