News for hackers. Stuff that matters.
Re:Why Const Doesn't Make C Code Faster by bstamour on Monday August 19, 2019 @09:57PM (#20743252)
If the initial object itself isn't const, then merely declaring the function parameter as a pointer to const won't guarantee that some other thread of execution won't change the value under your nose. Or if you have multiple pointer parameters, they can alias each other.

Indirection in C and C++ is a mess, but at least C has the "restrict" keyword. Best to program with value types whenever you can, and use pointers and references when you must.

Re:Why Const Doesn't Make C Code Faster by rrss on Monday August 19, 2019 @11:02PM (#20743498)
> won't guarantee that some other thread of execution won't change the value under your nose

My understanding is that compilers can always assume that there is no other thread involved, which (part of) why C11 atomics are necessary. Is that not the case?

Re:Why Const Doesn't Make C Code Faster by alexlarsson on Tuesday August 20, 2019 @06:08AM (#20745243)
Yes, but it can't assume any random function call it does doesn't use a different pointer to the same object which is not const. So, even if you don't cast const to non-const pointers you can run into the const value changing.
Re:Why Const Doesn't Make C Code Faster by bstamour on Wednesday August 21, 2019 @03:35PM (#20760783)
Concrete example:

  void foo(const int* const p1, int* const p2) {
    int a = *p1;
    *p2 += 42;
    int b = *p1;
    return a == b; // b = a + 42
  }
  int main() {
    int x = 0;
    return foo(&x, &x);
  }
Re:Why Const Doesn't Make C Code Faster by H8crilA on Tuesday August 20, 2019 @01:01AM (#20743971)
Yup, it's a data race (and therefore UB) to access the same memory from two different threads without synchronizing instructions (like mutexes, thread start/join, atomics).

It is allowed to concurrently read from const regions. But not concurrently read and write, and definitely not concurrently write.

Re:Why Const Doesn't Make C Code Faster by bstamour on Tuesday August 20, 2019 @07:55AM (#20745851)
You're right, but what I meant about it was that just because the variable is const, doesn't mean it can't change from somewhere else between reads. Even if you use synchronization to avoid data races, if you read the pointer to const twice in the function, it could change between the reads.
Re:Why Const Doesn't Make C Code Faster by SAI_Peregrinus on Tuesday August 20, 2019 @08:17AM (#20746012)
The most common example of that is probably a const volatile variable for the input data register of some peripheral. New data can come in at any time, and writes do nothing.