0

I'm trying to figure out when the standard allows using memset.

cppreference claims that memset can be used with trivially copyable types (modulo potentially overlapping subobjects — let's disregard those). The closest I could find while reading the standard (in particular, the C++17 draft publically available) is about memcpy — indeed, there is a clause that explicitly allows copying the representation of a trivially copyable object into a char array and back, but that's about it.

In fact, looks like the standard doesn't even define the semantics of memset with respect to the C++ entities it defines.

So, objects of which types am I allowed to memset, what are the restrictions on the value I'm allowed to memset them to and how to prove this?

0xd34df00d
  • 1,496
  • 1
  • 8
  • 17
  • 1
    Linked duplicate does not have the `language-lawyer` tag, but has answers that talk about what the standard strictly allows. – walnut Apr 14 '20 at 00:49
  • I’m not a lawyer, but it seems that `memset`ting an object should be valid as long as 1) the type is trivially copyable, and 2) the call results in a valid representation for that object. – numzero Apr 14 '20 at 00:49
  • @walnut thanks, the accepted answer there exactly answers my question. – 0xd34df00d Apr 14 '20 at 00:53

0 Answers0