ID:danglingTemporaryLifetime Using pointer to temporary.: C: conditionally change value of const char function parameter inside the function

Advertisements

Consider I have a C function:

MyFunction(const char *value, bool trigger)

Inside, depending on the value of the trigger variable, I’d like to either use a value provided to the function or simply overwrite it with some other const char string returned by a different function, e.g.:

MyFunction(const char *value, bool trigger) {
if (trigger) {
     value = anotherFunctionReturningConstChar().c_str();
}
// do processing here

}

What is the correct way to achieve this, considering the pointers and their type. As far as I understand, I cannot simply change the value of the function parameter and I need to use some third variable which should be set to either a value or to the result of anotherFunction.

I’m getting the following error right now in the static analyzer:
[ID:danglingTemporaryLifetime] Using pointer to temporary.

What does it mean and how can I overcome it?

>Solution :

No, you can assign to a function parameter exactly as you did. This doesn’t affect the caller in any way.

Leave a Reply Cancel reply