Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading