{"id":13745,"date":"2016-03-03T11:07:03","date_gmt":"2016-03-03T16:07:03","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=13745"},"modified":"2016-03-03T11:07:03","modified_gmt":"2016-03-03T16:07:03","slug":"why-safe-c-is-sometimes-unsafe-swift","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2016\/03\/03\/why-safe-c-is-sometimes-unsafe-swift\/","title":{"rendered":"Why Safe C Is Sometimes Unsafe Swift"},"content":{"rendered":"<p><a href=\"http:\/\/www.cocoawithlove.com\/blog\/2016\/02\/16\/use_it_or_lose_it_why_safe_c_is_sometimes_unsafe_swift.html\">Matt Gallagher<\/a>:<\/p>\n<blockquote cite=\"http:\/\/www.cocoawithlove.com\/blog\/2016\/02\/16\/use_it_or_lose_it_why_safe_c_is_sometimes_unsafe_swift.html\">\n<p>In this article, I&rsquo;ll look at a class of memory safety bug that occurred multiple times while I was writing <a href=\"http:\/\/www.cocoawithlove.com\/blog\/2016\/02\/02\/partial-functions-part-two-catching-precondition-failures.html\">the previous article<\/a>. This particular bug occurs only in Release builds and can occur even when your code has no occurrence of the word &ldquo;unsafe&rdquo; anywhere in it.<\/p>\n<p>[&#8230;]<\/p>\n<p>That is the simple answer to what&rsquo;s gone wrong in the Swift version: we tried to use a pointer to the first element of a tuple to read and write to the whole tuple. Creating a pointer to the first element of a larger structure and using that as a proxy for the whole structure is common in C and C++ but it&rsquo;s simply not allowed in Swift.<\/p>\n<p>[&#8230;]<\/p>\n<p>For the example in this article, after destructuring the tuple the Swift compiler realizes that &#8211; according to the rules of the Swift memory model &#8211; only the zeroth field of the <code>string1<\/code> and <code>string2<\/code> tuples are ever read so the initialization of the remaining fields is marked as a &ldquo;dead store&rdquo; and the dead fields 1-9 are omitted from the function entirely (never allocated on the stack).<\/p>\n<\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Matt Gallagher: In this article, I&rsquo;ll look at a class of memory safety bug that occurred multiple times while I was writing the previous article. This particular bug occurs only in Release builds and can occur even when your code has no occurrence of the word &ldquo;unsafe&rdquo; anywhere in it. [&#8230;] That is the simple [&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":[4],"tags":[45,46,138,71,901],"class_list":["post-13745","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-c","tag-languagedesign","tag-optimization","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/13745","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=13745"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/13745\/revisions"}],"predecessor-version":[{"id":13746,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/13745\/revisions\/13746"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=13745"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=13745"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=13745"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}