Import C headers in Swift

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

+8
malloc ios swift
source share
4 answers

Unix / POSIX materials are in the Darwin module.

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

malloc also present, but it will not bring you much benefit - it returns an opaque pointer.

+7
source share

You cannot import C headers. To import a C library (or Objective-C infrastructure) into Swift, it must be in a module. I do not know about the module that contains malloc.

0
source share

Have you studied using bridge header?

Create an .h file named -Bridging-Header-File.h Then specify it in the project build settings (in the "Swift Compiler" section, find "Objective C Bridging Header") with

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

Now malloc_size () should be available

0
source share

Do you want to use "malloc_size"? You do not need to import "malloc.h" for this, assuming that you either enable Foundation or Cocoa.

This works great on the playground:

 import Foundation let chars = BytePtr.alloc( 1000 ) let qty_of_bytes_allocated = malloc_size( chars ) println( qty_of_bytes_allocated ) 
0
source share

All Articles