Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import C headers in Swift

Tags:

malloc

ios

swift

I cannot import malloc/malloc.h to use malloc_size() to see the size of an object. Is it available in Swift or am I doing it wrong: import malloc/malloc.h

like image 993
Arbitur Avatar asked Jun 03 '14 04:06

Arbitur


2 Answers

The Unix/POSIX stuff is all in the Darwin module.

import Darwin

let r = rand()
qsort_b(buffer, 0, 10, sortFunc)

malloc is in there, too, but it won't do you much good — it returns an opaque pointer.

like image 195
rickster Avatar answered Nov 15 '22 09:11

rickster


Have you looked into using a bridging header?

Create a .h file named -Bridging-Header-File.h Then reference it in your projects build settings (under "Swift Compiler" look for "Objective C Bridging Header") with:

$(SRCROOT)/<Your-Project-Name>-Bridging-Header.h

Now malloc_size() should be available

like image 43
James Avatar answered Nov 15 '22 09:11

James