Is signature char an interface problem?

Suppose I have a function

void foo(char *) 

which internally must treat its input as a block of bytes with a trailing NUL (say, this is a hash function for strings). I can specify an unsigned char* argument in a function. I could also change the announcement to

 void foo(unsigned char *) 

Now, given that char , signed char and unsigned char are three different types , will this change the interface with any reasonable definition of the term "interface" in C?

(This question is intended to solve the discussion raised by another question. I have my own opinions, but I will not accept the answer until the β€œwinner” appears with the votes of others.)

+4
source share
5 answers

In accordance with ISO / IEC 9899: TC3,

  • calling a function through an expression of an incompatible type is undefined behavior (6.5.2.2 Β§9)
  • compatible function types must have compatible parameter types (6.7.5.3 Β§15)
  • compatible pointer types must point to compatible types (6.7.5.1 Β§2)
  • char , signed char and unsigned char are different base types (6.2.5 Β§14) and therefore incompatible (6.2.7 Β§ 1), which is also explicitly mentioned in footnote 35 on page 35

So this is clearly a change in the programming interface.

However, since char * , signed char * and unsigned char * will have identical representations and alignment requirements in any valid C language implementation, the binary interface will remain unchanged.

+4
source

Yes it is. Client code that was previously compiled will no longer compile (or, in any case, may generate new warnings), so this is a violation.

+3
source

I choose "C - none of the above."

Although this is not a direct answer to the question you asked, the correct solution to the situation seems quite simple and obvious to me: you should not use any of the above.

At least IMO, you really have a reason to do otherwise, your function should accept void * or (preferably) void const * . What you are looking for is basically an opaque pointer, and exactly what void * provides. The user does not need to know anything about the internal components of your implementation, and since any other type of pointer will implicitly convert to void * , this is one of the few features that also does not violate existing code.

+2
source

No, it is not. Any change in the client code is trivial (especially if it avoids the warning), and in practice in almost any C implementation, you will find that you do not even need to recompile, since pointers to char* and unsigned char* will be transmitted in the same way in the calling convention .

0
source

Is char * an implicit conversion to unsigned char *?

  • Yes - you have not violated your interface
  • No - you missed the implementation
    the details.
0
source

All Articles