HiveBrain v1.2.0
Get Started
← Back to all entries
patterncMajor

Why can't the C compiler optimize changing the value of a const pointer assuming that two pointers to the same variable would be illegal/UB?

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
pointerconstillegalwhychangingsamevaluetheassumingoptimize

Problem

Recently I stumbled over a comparison between Rust and C and they use the following code:
bool f(int a, const int b) {
*a = 2;
int ret = *b;
*a = 3;
return ret != 0;
}


In Rust (same code, but with Rust syntax), it produces the following Assembler Code:
cmp dword ptr [rsi], 0
mov dword ptr [rdi], 3
setne al
ret


While with gcc it produces the following:
mov DWORD PTR [rdi], 2
mov eax, DWORD PTR [rsi]
mov DWORD PTR [rdi], 3
test eax, eax
setne al
ret


The text claims that the C function can't optimize the first line away, because a and b could point to the same number. In Rust this is not allowed so the compiler can optimize it away.

Now to my question:

The function takes a const int* which is a pointer to a const int. I read this question and it states that modifying a const int with a pointer should result in a compiler warning and in the worst cast in UB.

Could this function result in a UB if I call it with two pointers to the same integer?

Why can't the C compiler optimize the first line away, under the assumption, that two pointers to the same variable would be illegal/UB?

Link to godbolt

Solution

The function int f(int a, const int b); promises to not change the contents of b through that pointer... It makes no promises regarding access to variables through the a pointer.

If a and b point to the same object, changing it through a is legal (provided the underlying object is modifiable, of course).

Example:

int val = 0;
f(&val, &val);

Code Snippets

int val = 0;
f(&val, &val);

Context

Stack Overflow Q#65993616, score: 50

Revisions (0)

No revisions yet.