{"id":20774,"date":"2018-03-02T11:25:43","date_gmt":"2018-03-02T16:25:43","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=20774"},"modified":"2018-03-02T11:27:33","modified_gmt":"2018-03-02T16:27:33","slug":"c-string-functions-in-swift-a-mallocy-story","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2018\/03\/02\/c-string-functions-in-swift-a-mallocy-story\/","title":{"rendered":"C String Functions in Swift: a malloc&rsquo;y Story"},"content":{"rendered":"<p><a href=\"http:\/\/www.alwaysrightinstitute.com\/swift-cstr\/\">Helge He&szlig;<\/a> (<a href=\"https:\/\/twitter.com\/helje5\/status\/946789633207209984\">tweet<\/a>):<\/p>\n<blockquote cite=\"http:\/\/www.alwaysrightinstitute.com\/swift-cstr\/\"><p>The pointer you get back points into a buffer, which doesn&rsquo;t exist anymore. That is why you need to be super careful when accessing C APIs.<\/p><p>[&#8230;]<\/p><p>Why does this [Objective-C] rarely result in an allocation? In the case above the <code>NSString<\/code> is an <code>NSConstantString<\/code> which is already backed by an UTF-8 string, and as mentioned above, a lot of <code>NSString<\/code>&rsquo;s are backed by UTF-8.<\/p><p>[&#8230;]<\/p><p>I was incorrectly assuming that Swift would create the static <code>String<\/code> in a way that is backed by an UTF-8 buffer, including the terminating 0 (because that byte is negligable). And more importantly, that the compiler would directly pass over the pointer to that cString buffer. [&#8230;] When using C API with Swift Strings (be it a simple <code>puts<\/code> or maybe libxml2),\nbe aware that such calls are <em>really<\/em> expensive (a malloc+free <em>per<\/em> call).<\/p><\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Helge He&szlig; (tweet): The pointer you get back points into a buffer, which doesn&rsquo;t exist anymore. That is why you need to be super careful when accessing C APIs.[&#8230;]Why does this [Objective-C] rarely result in an allocation? In the case above the NSString is an NSConstantString which is already backed by an UTF-8 string, and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"","apple_news_api_id":"","apple_news_api_modified_at":"","apple_news_api_revision":"","apple_news_api_share_url":"","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[],"tags":[45,69,571,54,138,71,901],"class_list":["post-20774","post","type-post","status-publish","format-standard","hentry","tag-c","tag-cocoa","tag-memory-management","tag-objective-c","tag-optimization","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/20774","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=20774"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/20774\/revisions"}],"predecessor-version":[{"id":20775,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/20774\/revisions\/20775"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=20774"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=20774"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=20774"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}