Home > Blockchain >  ID:danglingTemporaryLifetime Using pointer to temporary.: C: conditionally change value of const cha
ID:danglingTemporaryLifetime Using pointer to temporary.: C: conditionally change value of const cha

Time:11-05

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?

CodePudding user response:

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

CodePudding user response:

use MyFunction(void *value, bool trigger) where you hold a pointer to a function or to a const char* and then cast the value to the desired object but I think it is useless.

  • Related