#Pointer to temporary as function argument

5 messages · Page 1 of 1 (latest)

west otter
#

The following is my code

unsafe{
bindings::C_function_accepting_a_C_string(
            CString::new(a_str_slice)
                .expect("The passed string slice should not have internal 0 bytes")
                .as_ptr());
}

for which I get the warning "getting the inner pointer of a temporary 'CString'. This 'CString' is deallocated at the end of the statement, bind it to a variable to extend its lifetime.". As far as I know the statement ends after the function call returns, after which the temporary is not read from anymore. Thus this should be completely sound.

Is the warning just a result of a overcautious compiler which cannot know what I know or is there really a problem with my code?

deep nimbus
#

this lint is fairly primitive, so it doesn't understand this

#

Still, it would be better to just bind it to a variable

west otter
#

Thanks for the help