Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How much does the C standard library extensibility affect C++ programs?

Take this code:

int issuecode(int i)
{
  return 2 * i;
}

int main(int argc, char **argv)
{
  return issuecode(argc);
}

The way I understand it, if compiled as a C program, it will have undefined behaviour. I reason based on these standard quotes:

C99, 7.26 (or C11, 7.31)

The following names are grouped under individual headers for convenience. All external names described below are reserved no matter what headers are included by the program.

C99, 7.26.2 (or C11, 7.31.2)

Function names that begin with either is or to, and a lowercase letter may be added to the declarations in the <ctype.h> header.

C99, 7.1.3 (or C11, 7.1.3)

  1. Each header declares or defines all identifiers listed in its associated subclause, and optionally declares or defines identifiers listed in its associated future library directions subclause and identifiers which are always reserved either for any use or for use as file scope identifiers.

    [...]

    • All identifiers with external linkage in any of the following subclauses (including the future library directions) are always reserved for use as identifiers with external linkage.
  2. [...] If the program declares or defines an identifier in a context in which it is reserved (other than as allowed by 7.1.4), or defines a reserved identifier as a macro name, the behavior is undefined.

Based on the above, I believe the function name issuecode is actually reserved for use in <ctype.h>, and so the program technically has UB.

Question 0 (sanity check): Is my reading of the standard correct and the program's behaviour technically undefined?

Question 1: Will the program have UB if compiled as C++ code?

I believe the answer is "no," as from the following quotes, I'd say the "future library directions" of C are not part of the C++ standard library, but I am not really sure.

C++11, 21.7

  1. Tables 74, 75, 76, 77, 78, and 79 describe headers <cctype>, <cwctype>, <cstring>, <cwchar>, <cstdlib> (character conversions), and <cuchar>, respectively.

  2. The contents of these headers shall be the same as the Standard C Library headers <ctype.h>, <wctype.h>, <string.h>, <wchar.h>, and <stdlib.h> and the C Unicode TR header , respectively, with the following modifications:

None of the "following modifications" mentions the additional reserved identifiers. Table 74 is a taxative list of function names like isdigit and isalnum.

C++11, C.2

1. This subclause summarizes the contents of the C++ standard library included from the Standard C library. It also summarizes the explicit changes in definitions, declarations, or behavior from the Standard C library noted in other subclauses (17.6.1.2, 18.2, 21.7).

7. The C++ standard library provides 209 standard functions from the C library, as shown in Table 153.

Again, table 153 is a taxative list.

Question 2: Assuming I am wrong on question 1 and the program actually does have UB in C++ as well, would the following change affect this?

namespace foo {

  int issuecode(int i)
  {
    return 2 * i;
  }

}

using namespace foo;

int main(int argc, char **argv)
{
  return issuecode(argc);
}

Note: The standard quotes are taken from drafts N1256 (C99), N1570 (C11) and N3242 (C++11), which are the latest publically available drafts for the respective language versions.

like image 649
Angew is no longer proud of SO Avatar asked Jul 06 '13 15:07

Angew is no longer proud of SO


People also ask

What happens if standard library is not present in C?

Without the standard library, you're entire reliant on your own code, any non-standard libraries that might be available to you, and any operating system system calls that you might be able to interface to (which might be considered non-standard library calls).

What is library in C programming?

A library in C is a collection of header files, exposed for use by other programs. The library therefore consists of an interface expressed in a . h file (named the "header") and an implementation expressed in a . c file.

How many libraries does C have?

ANSI Standard. The ANSI C standard library consists of 24 C header files which can be included into a programmer's project with a single directive.


1 Answers

The following names are grouped under individual headers for convenience. All external names described below are reserved no matter what headers are included by the program.

There is a predefined list of functions that are reserved, if your function does not conflict name wise there is no issue.

Function names that begin with either is or to, and a lowercase letter may be added to the declarations in the <ctype.h> header.

The operative term there is may be added to the <ctype.h> header. The "is or to" bit is just guidance on organization of declarations.

So there never really was undefined behavior there...

As for C++ I think this follows the same idea take for example:

namespace foo{
  int isupper ( int c );
}

#include <cctype>
using namespace foo;
int main(void){
    isupper(92);
}

That should produce a compiler error because your function collides name wise with the C function, but due to namespaceing this is easily fixable by either appending an std:: or a foo:: to the beginning of the call.

like image 110
Mgetz Avatar answered Sep 30 '22 16:09

Mgetz