Change the value of a const pointer


Izrin

Recently, I stumbled across a comparison between Rust and C, 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 would generate the following assembly code:

    cmp      dword ptr [rsi], 0 
    mov      dword ptr [rdi], 3 
    setne al                    
    ret

When using 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 literal claims that the C function cannot optimize the first line because a and b may point to the same number. In Rust this is not allowed, so the compiler can optimize it away.

Now my question is:

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 the worst cast in UB.

If I call the function with two pointers to the same integer, will that cause UB?

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

Link to Godbolt : https://www.godbolt.org/z/j8EdG8

mg

The function int f(int *a, const int *b);guarantees that nothing will change b through that pointer ...it doesn't guarantee that the variable will be accessed through the pointer .a

If aand bpoints to the same object, it is legal toa change it (provided the underlying object can be modified, of course).

example:

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

Related


Change the value of a const pointer

Izrin Recently, I stumbled across a comparison between Rust and C, 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 would generate the following a

Change the value of a const pointer

Izrin Recently, I stumbled across a comparison between Rust and C, 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 would generate the following a

Change the value of a const pointer

Izrin Recently, I stumbled across a comparison between Rust and C, 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 would generate the following a

Change the value of a const pointer

Izrin Recently, I stumbled across a comparison between Rust and C, 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 would generate the following a

const pointer to address or pointer to value

username Are int const *aand const int *amean "a is an integer pointer to a constant value" and "a is a pointer to a constant address" respectively, or vice versa? This was asked me in an interview. After reading a lot about the same thing, I'm still very conf

const to pointer can change the object

enough A const reference ensures that you cannot change the object you are referencing. E.g: int i = 1; const int& ref = i; ref = 42; // error, because of a const reference However, if you use a reference to a pointer or unique_ptr, you can. example: class Ti

const to pointer can change the object

enough A const reference ensures that you cannot change the object you are referencing. E.g: int i = 1; const int& ref = i; ref = 42; // error, because of a const reference However, if you use a reference to a pointer or unique_ptr, you can. example: class Ti

const to pointer can change the object

enough A const reference ensures that you cannot change the object you are referencing. E.g: int i = 1; const int& ref = i; ref = 42; // error, because of a const reference However, if you use a reference to a pointer or unique_ptr, you can. example: class Ti

const to pointer can change the object

enough A const reference ensures that you cannot change the object you are referencing. E.g: int i = 1; const int& ref = i; ref = 42; // error, because of a const reference However, if you use a reference to a pointer or unique_ptr, you can. example: class Ti

const to pointer can change the object

enough A const reference ensures that you cannot change the object you are referencing. E.g: int i = 1; const int& ref = i; ref = 42; // error, because of a const reference However, if you use a reference to a pointer or unique_ptr, you can. example: class Ti

const to pointer can change the object

enough A const reference ensures that you cannot change the object you are referencing. E.g: int i = 1; const int& ref = i; ref = 42; // error, because of a const reference However, if you use a reference to a pointer or unique_ptr, you can. example: class Ti

How to change const char pointer to unique pointer?

Ning Hao So I tried to change my game, but I found a problem that I can't change: I have the following code: std::string fileContents = ""; const char* contentsPtr = fileContents.c_str(); I tried doing: const std::unique_ptr<char> contentsPtr = fileCont

Pass pointer by value or const ref?

Barry In short, is it better to pass pointers by value or const reference? I wrote a simple function to explore: void foo(int* v) { std::cout << *v << endl; } and void foo(int* const& v) { std::cout << *v << endl; } I tried looking at the generated assembly,

Parse: change the value of a pointer

username For example, I have two classes Playerand Gamea field Gamewith playera pointer to Player. As the Parse documentation states, it save's recursive, so I'm assuming I can save a playerby just saving a game, like: let player = PFObject(className: "Player"

Parse: change the value of a pointer

username For example, I have two classes Playerand Gamea field Gamewith playera pointer to Player. As the Parse documentation states, it save's recursive, so I'm assuming I can save a playerby just saving a game, like: let player = PFObject(className: "Player"

Change the value of a pointer by address

Willie Freddy This week, we were tasked with creating a program that: Program to display the address and value of an array User enters the appropriate address to change The user enters a new value for the address I was able to figure out how the pointer works,

Change the value of a pointer to

Sorter So I have a similar function (simplified example): void somefunction(structuraltype *something) { something->variable = 6; } As you can see I can easily access the value using -> But I realize that I have to use a pointer to a pointer, not a sing

pointer to const (or pointer to const)

Wengers I have the following code: #include <iostream> int main(){ int v1 = 20; int *p1 = &v1; int **p2 = &p1; return 0; } What I'm trying to do here is point one pointer to another and it works fine in this case. I make p1 point to a cons

change value of pointer to pointer in c function

swing machine I pass in a double pointer to a function, and the function allocates space on the heap and redirects the double pointer to that memory location. Then on the main side we should be able to access the memory location and its contents my code: void

C++ modify const pointer reference value

Ernestas I came across a similar snippet of code today, and it struck me as odd. I did a small experiment myself as shown below. Why does the first function stuffwith a shared pointer allow to modify the value but the second does not? #include <memory> void s

C++ modify const pointer reference value

Ernestas I came across a similar snippet of code today, and it struck me as odd. I did a small experiment myself as shown below. Why does the first function stuffwith a shared pointer allow to modify the value but the second does not? #include <memory> void s

C++ modify const pointer reference value

Ernestas I came across a similar snippet of code today, and it struck me as odd. I did a small experiment myself as shown below. Why does the first function stuffwith a shared pointer allow to modify the value but the second does not? #include <memory> void s

C++ modify const pointer reference value

Ernestas I came across a similar snippet of code today, and it struck me as odd. I did a small experiment myself as shown below. Why does the first function stuffwith a shared pointer allow to modify the value but the second does not? #include <memory> void s

C++ modify const pointer reference value

Ernestas I came across a similar snippet of code today, and it struck me as odd. I did a small experiment myself as shown below. Why does the first function stuffwith a shared pointer allow to modify the value but the second does not? #include <memory> void s

Change the value of a pointer passed by reference

Philip I have a strange problem. I'm thinking of the following example, where I'm passing a pointer by reference, so if I change the pointer address of the parameter, the initial pointer should also change the address. This does happen. However, the value of a

Change the value of a pointer passed by reference

Philip I have a strange problem. I'm thinking of the following example, where I'm passing a pointer by reference, so if I change the pointer address of the parameter, the initial pointer should also change the address. This does happen. However, the value of a

access pointer to change its value

Guerlando OC I've been trying to write a small matrix framework in C. The problem is that the components accessing the matrix are modifying it in some way. You'll see that the elements of the matrix achange weirdly after being passed to matrix_scalar_multiply.

Change of pointer value in a single run

learning_cpp I'm trying to build an Oct tree structure, but the value of the pointer seems to have changed during runtime. insert() simulates insertion by randomly adding children to the parent node, and show() is used to display all elements on the path. Each