1

This question and many more are among the numerous questions on this site that ask if a call to memcpy() is valid with a length\size specified with a zero value. When answering, everyone quotes the current C standard (in this case C17 ISO/IEC9899:2017 page 283 ),

Where an argument declared as size_t n specifies the length of the array for a function, n can have the value zero on a call to that function. Unless explicitly stated otherwise in the description of a particular function in this subclause, pointer arguments on such a call shall still have valid values, as described in 7.1.4. On such a call, a function that locates a character finds no occurrence, a function that compares two character sequences returns zero, and a function that copies characters copies zero characters.

However, this is from C standard, not quoted from the C++ standard.

Where in the current C++ standard (i.e. C++17 ISO/IEC 14882) is this same definition listed? C and C++ have two different standards (and languages) and from my understanding, you cannot quote one and expect that same rule/behavior to be present in the other standard.

If this quotation from the C standard is valid in C++ without explicitly stating it in the standard, can someone then provide a source that backs up this connection between C and C++?

Code Doggo
  • 2,146
  • 6
  • 33
  • 58
  • memcpy's man page says that the function "Copies the values of `num` bytes from the location pointed to by source" ... and 0 is a perfectly valid value of `num`. Is that sufficient? http://www.cplusplus.com/reference/cstring/memcpy/ – Jeremy Friesner Nov 14 '19 at 05:26
  • @JeremyFriesner According to [International Organization for Standardization's (ISO) website on C++](https://isocpp.org/std/the-standard), "*sites like cppreference.com and cplusplus.com [are not authoritative and are used for unofficial references].*" – Code Doggo Nov 14 '19 at 05:31

3 Answers3

6

The C++17 standards says this about the C standard library:

1 The C++ standard library also makes available the facilities of the C standard library, suitably adjusted to ensure static type safety.

2 The descriptions of many library functions rely on the C standard library for the semantics of those functions. In some cases, the signatures specified in this International Standard may be different from the signatures in the C standard library, and additional overloads may be declared in this International Standard, but the behavior and the preconditions (including any preconditions implied by the use of an ISO C restrict qualifier) are the same unless otherwise stated.

As to your question,

If this quotation from the C standard is valid in C++ without explicitly stating it in the standard

The answer is "yes".

R Sahu
  • 204,454
  • 14
  • 159
  • 270
1

[library.c]/2, emphasis mine:

The descriptions of many library functions rely on the C standard library for the semantics of those functions. In some cases, the signatures specified in this document may be different from the signatures in the C standard library, and additional overloads may be declared in this document, but the behavior and the preconditions (including any preconditions implied by the use of an ISO C restrict qualifier) are the same unless otherwise stated.

[cstring.syn]/1:

The contents and meaning of the header <cstring> are the same as the C standard library header <string.h>.

cpplearner
  • 13,776
  • 2
  • 47
  • 72
1

The C library is sort of baked into C++. As said in [intro.scope] from the C++17 standard (emphasis mine):

  1. C++ is a general purpose programming language based on the C programming language as described in ISO/IEC 9899:2011 Programming languages — C (hereinafter referred to as the C standard). In addition to the facilities provided by C, C++ provides additional data types, classes, templates, exceptions, namespaces, operator overloading, function name overloading, references, free store management operators, and additional library facilities.

And again, in [intro.refs]:

  1. The library described in Clause 7 of ISO/IEC 9899:2011 is hereinafter called the C standard library.1

1) With the qualifications noted in Clauses 21 through 33 and in C.5, the C standard library is a subset of the C++ standard library.

So everything that is in the C standard library is in C++.