Some "safe" languages like Swift and Go offer "unsafe" pointers for use with APIs written in languages like C and Objective-C. Questions using this tag should also have the relevant language tag, e.g. [swift] or [go].
Questions tagged [unsafe-pointers]
250 questions
3
votes
2 answers
Dynamically setting value on array of pointers to integers
I have a multidimentional array of pointers to integer (of unknown rank) being passed into my function as such:
public unsafe static void MyMethod(Array source, ...)
{
//...
}
Multidimensional arrays of pointers are being…

Robert Venables
- 5,943
- 1
- 23
- 35
2
votes
0 answers
Passing pointer argument to unsafe.Pointer: found bad pointer in Go heap
What are the problems with using
Approach 1:
func Encode(v *float32) []byte {
return unsafe.Slice((*byte)(unsafe.Pointer(v)), 4)
}
over
Approach 2
func Encode(v float32) []byte {
return unsafe.Slice((*byte)(unsafe.Pointer(&v)), 4)
}
Both…

Arjun Sunil Kumar
- 1,781
- 3
- 28
- 46
2
votes
2 answers
Unsafe slicing a 2D array with ReadOnlySpan
On .Net Core 3.1, I have many large 2D arrays where I need to operate on a slice of a single row in the array. The same slice may be used by multiple operations so I would like to perform the slice just once and reuse the slice.
The sample code…

ElGordo
- 193
- 1
- 7
2
votes
1 answer
Data via Buffer Post Xcode 11.5 Update
What I Have:
Referencing Apple's Chroma Key Code, it states that we can create a Chroma Key Filter Cube via
func chromaKeyFilter(fromHue: CGFloat, toHue: CGFloat) -> CIFilter?
{
// 1
let size = 64
var cubeRGB = [Float]()
//…

impression7vx
- 1,728
- 1
- 20
- 50
2
votes
2 answers
How to pass *uint16 pointer to windows.CreateFile() in Golang
I am trying to create a file using windows.CreateFile() function (for reference please see https://godoc.org/golang.org/x/sys/windows#CreateFile and https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilew ) in Golang…

Rodrigo
- 93
- 1
- 8
2
votes
1 answer
How to get an array from a C function in Swift?
I am working with a C function in my Swift code that outputs an array. The function doesn't return an array because, apparently in C, functions are discouraged from returning arrays. Therefore, the function takes an in-out parameter (as a pointer)…

lurning too koad
- 2,698
- 1
- 17
- 47
2
votes
2 answers
how to properly extract the array of numbers from an image in swift?
I'm trying to extract the array of numbers from a UIImage in swift but at the end I got only a bunch of zeros no useful information at all.
that's the code I wrote to try accomplishing this.
var photo = UIImage(named: "myphoto.jpg")!
var withAlpha…

Lucas
- 746
- 8
- 23
2
votes
1 answer
How to copy a pointer to an array in Swift?
I'm basically trying to wrap a C structure within a Swift class.
The Swift class has an instance property of type of the C structure.
The C structure contains several properties which are of type const char *.
In order to assign values to the…

j3141592653589793238
- 1,810
- 2
- 16
- 38
2
votes
1 answer
Why is there no segfault when accessing a variable that has gone out of scope using unsafe Rust?
I came across this strange phenomenon while playing with unsafe Rust. I think this code should make a segmentation fault but it does not. Am I missing something?
I tried to set a pointer to a variable with a shorter lifetime and then dereference…

Turtle Turtleston
- 35
- 5
2
votes
1 answer
Custom Weak/Strong Reference Pointers
I am creating my own implementation of a weak/strong reference pointer relationship and I am confused to the configuration. When I have a class that contains the strong reference, and I want to set the strong pointer to another class that has a weak…

chadb
- 1,138
- 3
- 13
- 36
2
votes
3 answers
How to read bytes of struct in Swift
I am working with a variety of structs in Swift that I need to be able to look at the memory of directly.
How can I look at a struct byte for byte?
For example:
struct AwesomeStructure {
var index: Int32
var id: UInt16
var stuff: UInt8
//…

Brandon M
- 349
- 2
- 20
2
votes
1 answer
Directly assign UnsafePointer
Can somebody please explain to me what I'm missing here?
Given
let index: Int32 = 100
Why is this not okay:
// Use of extraneous '&'
let ptr = &index // Type inference?
Or even:
// Use of extraneous '&'
let ptr: UnsafePointer = &index
But…

Brandon M
- 349
- 2
- 20
2
votes
1 answer
Unsafepointer adjustment for Swift 4
I have not found a solution how i can adjust the following lines (error) to Swift 4. Regarding to documents, withUnsafePointer method should be used but i could not find a way to add addressBytes.bytes into code.
func netServiceDidResolveAddress(_…

Sunrise17
- 379
- 3
- 18
2
votes
1 answer
How do I handle CFSocketCallBackType.dataCallback in Swift?
All documentation and examples say that if a socket's CFSocketCallBack callout is given a .dataCallback as its second parameter (callbackType), that implies that the fourth one (data) can be cast to a CFData object containing all the data pre-read…

Ky -
- 30,724
- 51
- 192
- 308
2
votes
1 answer
UnsafePointer not working
The following unit test always fails as some UnsafePointer equal some other. This behavior I observe for at least 3 strings.
I'm using Xcode Version 9.1 (9B55) on High Sierra Version 10.13.1 (17B48).
func testUnsafePointer() {
let a = "aaa"
…

swift616
- 23
- 3