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
1
vote
1 answer
How to create UnsafePointer from existing CGRect
How to create UnsafePointer?
Trying let mediaBoxPtr = UnsafePointer(mediaBox) but fails
func PDFImageData(filter: QuartzFilter?) -> NSData? {
let pdfData = NSMutableData()
let consumer = CGDataConsumerCreateWithCFData(pdfData);
var…

Marek H
- 5,173
- 3
- 31
- 42
1
vote
2 answers
Extension for Generic Type `UnsafeMutablePointer`
I'd like to create an extension for UnsafeMutablePointer that only affects UnsafeMutablePointer...
I understand that these instructions are pertinent, but I'm not sure how:
When you extend a generic type, you do not provide a type parameter…

Dan Rosenstark
- 68,471
- 58
- 283
- 421
1
vote
1 answer
UnsafeMutablePointer: What's the risk?
I'm receiving one of these in a callback from an Objective-C library: UnsafeMutablePointer
I'm able to parse it. I'm also able to create one to send it back to the library, but: What are the risks of working with the "unsafe" type? How do I…

Dan Rosenstark
- 68,471
- 58
- 283
- 421
1
vote
1 answer
Point an UnsafeMutablePointer to itself
In C, it is valid to have a pointer point to the memory location allocated to the pointer:
static void * SomeContext = &SomeContext;
I have seen this used in Objective-C codebases with KVO by using this pointer as the context in…

JAL
- 41,701
- 23
- 172
- 300
1
vote
1 answer
How do I convert my swift class to an UnsafePointer like the __bridge in objective-c
I'm trying to implement a swift bridge to the SecureTransport C libraries. I 'think' I can pass in anything as the connection as long as I know how to read stuff from it in my sslReadCallback/sslWriteCallback implementations. This is the assumption…

Mike S
- 4,092
- 5
- 35
- 68
1
vote
3 answers
unexpectedly found nil while unwrapping an Optional value while reading from DS with fromCString
I am reading from a dbtable and get an error at a specific position of the table. My sql is ok, because I could already read from the same table, but at a specific row I get an error and I would like to know howto handle this error. I am not looking…

mcfly soft
- 11,289
- 26
- 98
- 202
1
vote
1 answer
UnsafePointer to Last Array Element in Swift
I'm attempting to use the vDSP_conv function in Accelerate here. One of the arguments to vDSP_conv is a const float *__F that "needs to point to the last vector element". I'm not extremely familiar with using pointers in Swift, so I'm at a loss for…

hyperdelia
- 1,105
- 6
- 26
1
vote
4 answers
Do classes with uninitialized pointers have undefined behavior?
class someClass
{
public:
int* ptr2Int;
};
Is this a valid class (yes it compiles)? Provided one assigns a value to ptr2Int before dereferencing it, is the class guaranteed to work as one would expect?

user383352
- 5,033
- 4
- 25
- 20
1
vote
1 answer
Using AudioBufferList with Swift once again
Refering to Using AudioBufferList with Swift
I found the following solution here on Stack Overflow for playing some sound with audio unit. My problem here is that I'm not able to put actual data in my buffer like sin wave data. I tried it with…

easysaesch
- 159
- 1
- 14
1
vote
2 answers
How to assign (byte* + offset) to byte[]?
I have a struct:
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public unsafe struct FieldIndex {
public fixed byte Meta[16];
}
The first byte is TypeCode. The remaining 15 bytes is a UTF8-encoded string.
I'm using the following to fill the…

IAbstract
- 19,551
- 15
- 98
- 146
1
vote
2 answers
Why Does This Black and White Bitmap to Bool Array create the correct images but set the wrong amount of bools (C#)
Here is a function that takes a transparent and white image and attempts to turn it into a bool array.
My unit test code gives me 2 images that I would expect (see below) but the "numberOfMasked" is always higher then I expect. For example if the…

chrispepper1989
- 2,100
- 2
- 23
- 48
1
vote
1 answer
Still using "public String(char* value)" nowadays?
I came across(inside(definition) of the string "class"(.NET)):
public String(char* value);
What is written in the commentary:
// Parameters:
// value:
// A pointer to a null-terminated array of Unicode characters.
My…

user254197
- 883
- 2
- 9
- 31
1
vote
0 answers
Converting UIImage to and from [[UIColor]]?
I'd like to convert my UIImage to and from a 2-dimensional UIColor array. I have only found ways to do this with UnsafePointer.
Is there a way to do this in Swift without using pointers?

vrwim
- 13,020
- 13
- 63
- 118
1
vote
2 answers
EXC_ARM_DA_ALIGN when reading from NSData in Swift
I have the following class:
class RawDataArray {
var data: NSData!
init(filePath: String) {
data = NSData(contentsOfFile: filePath)
}
func read(offset: Int) -> T {
return UnsafePointer(data.bytes +…

Eduardo
- 8,362
- 6
- 38
- 72
1
vote
1 answer
Unable to Publish WCF Service with Unsafe code in Visual Studio 2013
This might be same problem as mentioned in the following link
Publish web application with unsafe code
I have a wcf service with a block of Unsafe code.
I have Allowed Unsafe Code in My Project properties.
The Application is able build properly…

Karthik Dheeraj
- 1,039
- 1
- 13
- 25