Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does changing f(mystruct *a) to f(const mystruct *a) breaks API/ABI in C?

1: void f(mystruct *a)
2: void f(const mystruct *a)

Does changing the function signature from 1->2 break API/ABI in C?
How about changing 2->1?

like image 354
Dima Avatar asked Feb 22 '11 20:02

Dima


2 Answers

From C99 standard 6.2.5/26 "Types":

pointers to qualified or unqualified versions of compatible types shall have the same representation and alignment requirements.

So the ABI/API should not be affected going from 1 to 2. (The API doesn't change because a pointer to a non-const-qualified type may be converted to a pointer to a const-qualified version of the type - 6.3.2.3/2 "Conversions - Pointers").

However, if you go from 2 to 1, then the API changes because a pointer to a const object cannot be implicitly converted to a pointer to a non-const object. The following code would compile under version 2, but would not compile under version 1:

static const mystruct foo;

f(&foo);
like image 127
Michael Burr Avatar answered Sep 27 '22 20:09

Michael Burr


Other than stated in the two previous answers going from 1->2 may or may not break the API. This depends on the base type mystruct. The API would break if other than what the name indicates mystruct would be a typedef to an array type.

typedef struct toto mystruct[1];

For such a beast

mystruct A;
f(&A);

the call to f would be valid before the API change but invalid afterwards.

like image 26
Jens Gustedt Avatar answered Sep 27 '22 20:09

Jens Gustedt