DenseVector_Double generates warning

Hello


The documentation for DenseVector_Double includes an example as shown:


var values = [2.20, 2.85, 2.79, 2.87]
let vector = DenseVector_Double(count: Int32(values.count),
data: &values)

stride(from: 0, to: vector.count, by: 1).forEach {i in
print(vector.data[Int(i)])
}


Previously, this ran without difficulty. However recently (Swift 5.1 or 5.2?) it generates a warning:

Inout expression creates a temporary pointer, but argument 'data' should be a pointer that outlives the call to 'init(count:data:)'


I'm still new to Swift. Can someone explain, using this same example, how to avoid the warning?


Thanks.

Answered by Claude31 in 416768022

To find yopu own post, just click on "Content" at the top of this page.

Then select Authored on the left.


You will see all your posts.

That's very useful when your post has been moved to another part of the forum by moderator.

This seems to be the same question as your previous quesion:

https://forums.developer.apple.com/thread/131952


You have an answer there.


If you do not understand it well, you can add a reply to the answer.


Can someone explain, using this same example, how to avoid the warning?

If you mean using the example code exactly as is, you cannot. The sample code is broken and you should not use it.

Yes, sorry. I've not previously used the forum, and could not find the original question, so posed it again.

I've found it now. Thank you.

Accepted Answer

To find yopu own post, just click on "Content" at the top of this page.

Then select Authored on the left.


You will see all your posts.

That's very useful when your post has been moved to another part of the forum by moderator.

Thank you, this helps, and certainly works with the documented example!

(As you say, it depends on how the data are being used, and my example is more complex than the one in the documentation...

I need to try to understand how this works using the newly defined dense vector later in the code, where it now does not seem think the vector has been defined. For example, in the code you provided, the vector is printed, via the stride command, within the braces of the values.withUnsaftMutableBufferPointer construct. But if the brace ends before the stride command, the error is that "vector" is unresolved. I think my lack of understanding is from coming from Fortran to Swift, with need of0 more study of structs and classes...)

Anyway, many thanks!

Don't forget to close the 2 threads by now.


Good continuation.

DenseVector_Double generates warning
 
 
Q