Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does const char* alias with char*?

I am aware that not all types alias with each other in c. You cannot write to an address with an lvalue of one type, then read from it via an lvalue of another type.

My question is if same system applies to const and non-const objects.

Example:

char func(char *a, const char *b)
{
     *a = '0';
     return *b;//undefined behavior?
}

int main()
{
     char a = 5;
     return (int)func(&a, &a);
}
like image 326
Badasahog Avatar asked Feb 23 '26 03:02

Badasahog


2 Answers

What matters is the so-called effective type of the original object, which most of the time is the declared type of the object, in this case char a;. And how you access that one by de-referencing a pointer to it, a so called "lvalue access". *b would be a "lvalue expression" since the result of the * operator is an "lvalue" (which basically means: an object allocated at an address).

C23 6.5.1 states:

An object shall have its stored value accessed only by an lvalue expression that has one of the following types:

  • a type compatible with the effective type of the object,
  • a qualified version of a type compatible with the effective type of the object,
    ...
  • a character type.

The above is often called the "strict aliasing rule". From that we can learn:

  • A char* de-referenced is a type compatible with the effective type of the object (char in this case). So *a = 0 is fine.
  • A const char* de-referenced is a qualified version of that, const is a type qualifier. So *b is fine too.
  • Character types have a special aliasing rule in C as per the above quote (and as specified in 6.3.3). Any object can be inspected byte per byte through a lvalue access of a de-referenced character pointer. This means that a character pointer specifically may alias any other object pointer.

So yes, both pointers in your function alias, since one is the qualified version of the other. And besides they would also alias anyway, if at least one of them was a character pointer.

There is no undefined behavior in your code.

like image 110
Lundin Avatar answered Feb 25 '26 17:02

Lundin


Does const char* alias with char*?

Your sample code aliases char with const char, not char * with const char *. That is, the a defined in main is defined with a type of char and is accessed in return *b; with a type of const char. It is the actual type used to interpret the memory that is relevant, not the type of pointer used to calculate the location in memory.

Per the aliasing rules in C 2024 6.5, const char may access char for two reasons:

  • A qualified version of a type may alias the type.

  • Any character type (char, signed char, unsigned char, and qualified versions of them) may alias any object type.

If you had aliased an int using const int, that would be defined because for the first reason, but not the second.

If you had aliased a const int using int, that would not be defined, because int is neither a character type nor a qualified version of const int.

like image 21
Eric Postpischil Avatar answered Feb 25 '26 16:02

Eric Postpischil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!