Is this code causes UB: void foo(void** pp) { void* p = pp; // Do something with p } int…
- User
Is this code causes UB: void foo(void** pp) { void* p = *pp; // Do something with p }
int main() { void* p const = NULL; foo((void**)&p); // p has been reassigned }
- Assistant
No, this code does not cause undefined behavior (UB).
In the
foofunction, avoid*pointerpis being assigned the value of the variablepp, which is avoid**pointer. Then, some operations are performed onp. As long as the operations are valid for avoid*pointer, there won't be any UB.In the
mainfunction, avoid*pointerpis declared asconstand initialized toNULL. Then, a call is made to thefoofunction by passing the address ofpusing a typecast tovoid**. This is a valid operation becausepis a non-constvoid*pointer, andfooexpects avoid**pointer.After the
foofunction is called,pmay have been modified insidefoo, but sincepis not of constant type, there is no problem with reassigning its value. Therefore, there is no UB in this code. - User
So is it legal to reassign const pointer (void * const) by passing it into a function that reassigns it?
- Assistant
No, it is not legal to reassign a
constpointer (void* const) by passing it into a function that reassigns it.In the given code,
pis declared as aconstpointer (void* const) in the main function. This means that the value ofpcannot be modified. However, in thefoofunction, thevoid*pointerpis assigned a new value through the*ppexpression, which dereferences thevoid**pointerpp.Attempting to modify the value of a
constpointer is undefined behavior (UB). It violates the const-correctness principle and can result in unpredictable and unexpected behavior.To avoid UB, you should not attempt to modify the value of a
constpointer within a function. If you need to modify the value of a pointer inside a function, you should declare it as a non-constpointer. - User
So why did you tell me that the code is fine
- Assistant
I apologize for the confusion caused by my previous response. You are correct, there is a mistake in my previous explanation.
In the given code, declaring
pasvoid* constin themainfunction means that the pointer itself (p) is constant and cannot be reassigned to point to a different memory location. However, the memory location thatppoints to can still be modified.Passing
&p(the address ofp) into thefoofunction as avoid**is not valid. Attempting to modify aconstpointer through a cast can also lead to undefined behavior.To correctly modify the value of
pinside thefoofunction, you should change the declaration ofpin themainfunction tovoid* p = NULL;(withoutconst), and then pass&p(the address ofp) into thefoofunction asvoid**.I apologize for the confusion caused by my earlier response, and thank you for bringing it to my attention.
- User
What if I don't want to remove const qualifier but still want to reassign it at some point
- Assistant
If you have a
constpointer (void* const) and you want to reassign it at some point, you'll need to create a temporary non-const pointer, modify its value, and then assign it back to the originalconstpointer.Here's an example:
void foo(void* const* pp) { void* p = *pp; // Do something with p p = /* new value */; // Modify p *pp = p; // Assign the modified value back to the const pointer } int main() { void* const p = /* initial value */; foo(&p); // The value of p has been modified }In this code,
footakes avoid* const*parameter, which is a pointer to aconstpointer. Insidefoo, a temporary non-constvoid*pointerpis assigned the initial value of*pp.After making modifications to
p, you can assign the modified value back to the originalconstpointer*pp. This is valid because you're not modifying the originalconstpointer itself, but rather updating the value it points to.By following this approach, you can reassign the value of a pointer indirectly without violating const-correctness.
- User
Can I have a pointer to const pointer?
- Assistant
Yes, you can have a pointer to a
constpointer in C++. It's a common practice when you want to modify the value of theconstpointer indirectly or pass a pointer to aconstpointer to a function.Here's an example:
void foo(const char** pptr) { const char* p = *pptr; // Do something with p *pptr = /* new value */; // Modify the const pointer indirectly } int main() { const char* p = "Hello"; const char** pptr = &p; // Pointer to a const pointer foo(pptr); // The value of p has been modified indirectly }In this code,
footakes aconst char**parameter, which is a pointer to aconstpointer tochar. Insidefoo, a temporaryconst char*pointerpis assigned the initial value of*pptr.You can modify the value of the
constpointer indirectly by assigning a different value to*pptr. This modification propagates to the originalconstpointerpin themainfunction.Using a pointer to a pointer allows you to modify the pointer indirectly while still preserving the qualification on the pointer itself.