{"id":11,"date":"2017-05-13T08:44:28","date_gmt":"2017-05-13T15:44:28","guid":{"rendered":"http:\/\/codingrestart.com\/?p=11"},"modified":"2017-05-15T17:31:56","modified_gmt":"2017-05-16T00:31:56","slug":"when-is-c-pointer-valid","status":"publish","type":"post","link":"https:\/\/codingrestart.com\/home\/when-is-c-pointer-valid\/","title":{"rendered":"When is C pointer valid?"},"content":{"rendered":"<p>C is largely based on raw pointers and one of the most often asked questions is when is a pointer a valid one. The reason is that dereferencing an invalid pointer generates an access violation exception (0xC0000005) and if not caught, the program crashes.<\/p>\n<p>You quickly learn there is <code>IsBadReadPtr<\/code> function available and its variants. You look at the name and think you found the best way to check the pointer&#8217;s validity. That is until you notice, that Microsoft documentation says these functions are obsolete and <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/windows\/desktop\/aa366713(v=vs.85).aspx\">warns<\/a> against using them in no uncertain terms. And Raymond Chan from Microsoft provides details at his blog <a href=\"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/20070625-00\/?p=26283\">The Old New Thing<\/a>.<\/p>\n<p>Then, you think this is such basic C\/C++ question, there must be a way to do it. You start searching the Internet and slowly come to realize, that there is no way to check validity of raw pointers.<\/p>\n<p>However, there are best practices how to guard against invalid pointers.<\/p>\n<h3>Best practices to guard against bad pointers<\/h3>\n<ul>\n<li>Initialize all pointers to <code>nullptr<\/code>.<\/li>\n<li>After deleting an object, set its pointer to <code>nullptr<\/code>.<\/li>\n<li>Check pointer for non-zero value before dereferencing it.<\/li>\n<\/ul>\n<p>The above best practices will not prevent you from dereferencing invalid raw pointers altogether. For example, it does not prevent somebody from passing an invalid pointer through the API. In that situation, you have to accept GIGO, garbage in garbage out paradigm.<\/p>\n<h3>How to check if pointer has non-zero value?<\/h3>\n<p>There are few ways of checking if a pointer points to nothing. If it does not, it has a non-zero value. Still does not make it valid, but you have done your best.<\/p>\n<ul>\n<li><code>if (p == 0)<\/code>: Check against number 0 which is typed as int. OK approach.<\/li>\n<li><code>if (p == NULL)<\/code>: NULL is macro defined as 0. Slightly better, makes your application more readable.<\/li>\n<li><code>if (p == nullptr)<\/code>: Check against C++11 keyword. Best, clean approach.<\/li>\n<\/ul>\n<p>The above triad is nicely explained by Kate Gregory in her &#8220;C++ Fundamentals&#8221; class on <a href=\"https:\/\/app.pluralsight.com\/player?course=cpp-fundamentals&amp;author=kate-gregory&amp;name=modern-cpp-06-pointers&amp;clip=1&amp;mode=live\">Pluralsight<\/a>, in chapter &#8220;Pointers&#8221;. For another take on the same read <a href=\"http:\/\/www.stroustrup.com\/bs_faq2.html#null\">Bjarne Stroustrup<\/a>.<\/p>\n<h3>Conclusion<\/h3>\n<p>There is no good way to check if a raw pointer is valid or not, in other words if it can be safely dereferenced or not. You should seriously consider not using raw pointers in your code and start using smart pointers <code>unique_ptr<\/code> and <code>shared_ptr<\/code> from C++11.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>C is largely based on raw pointers and one of the most often asked questions is when is a pointer a valid one. The reason is that dereferencing an invalid pointer generates an access violation exception (0xC0000005) and if not caught, the program crashes. You quickly learn there is IsBadReadPtr function available and its variants. &hellip; <a href=\"https:\/\/codingrestart.com\/home\/when-is-c-pointer-valid\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;When is C pointer valid?&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[2],"tags":[],"class_list":["post-11","post","type-post","status-publish","format-standard","hentry","category-cc"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/paAAlH-b","jetpack-related-posts":[{"id":30,"url":"https:\/\/codingrestart.com\/home\/why-is-smart-pointer-better\/","url_meta":{"origin":11,"position":0},"title":"Why is smart pointer better than a raw one?","author":"Viktor Sanek","date":"May 21, 2017","format":false,"excerpt":"In a previous post\u00a0I suggested that raw pointers should be replaced by smart pointers. This post explains why smart pointers are better than raw ones and how to convert an existing code using raw pointers to use smart pointers. For the sake of this post, when I refer to smart\u2026","rel":"","context":"In &quot;C\/C++&quot;","block_context":{"text":"C\/C++","link":"https:\/\/codingrestart.com\/home\/category\/uncategorized\/cc\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1,"url":"https:\/\/codingrestart.com\/home\/hello-programmers\/","url_meta":{"origin":11,"position":1},"title":"Hello programmers!","author":"Viktor Sanek","date":"May 7, 2017","format":false,"excerpt":"Welcome to CodingRestart.com, blog for seasoned developers that want to upgrade their skills. If any of the following is true for you, this site is for you: You feel threatened by younger colleagues, because you do not know popular programming languages and buzzwords they use. You think Kernighan and Ritchie's\u2026","rel":"","context":"Similar post","block_context":{"text":"Similar post","link":""},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":69,"url":"https:\/\/codingrestart.com\/home\/ml-msdn-and-xavier\/","url_meta":{"origin":11,"position":2},"title":"ML: MSDN and Xavier","author":"Viktor Sanek","date":"January 19, 2019","format":false,"excerpt":"Microsoft\u2019s MSDN magazine for its development community has been publishing quite a few introductory articles to Machine Learning (ML) over the past few months. January's issue emphasizes ML with another series of articles, albeit with differing qualities. I liked this quote from the editorial \"Advancing AI\": \u201dML is a huge\u2026","rel":"","context":"In &quot;Machine Learning&quot;","block_context":{"text":"Machine Learning","link":"https:\/\/codingrestart.com\/home\/category\/machine-learning\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":51,"url":"https:\/\/codingrestart.com\/home\/really-old-geek\/","url_meta":{"origin":11,"position":3},"title":"Really Old Geek","author":"Viktor Sanek","date":"December 28, 2018","format":false,"excerpt":"The New York Times published an article about Donald Knuth \u201cThe Yoda of Silicon Valley\u201d. The article might idolize his work and impact, but provides a great overview of his work. Mr. Knuth is a living legend in the field of computer science, known mainly for his seminal book on\u2026","rel":"","context":"In &quot;C\/C++&quot;","block_context":{"text":"C\/C++","link":"https:\/\/codingrestart.com\/home\/category\/uncategorized\/cc\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":77,"url":"https:\/\/codingrestart.com\/home\/kate-matsudaira\/","url_meta":{"origin":11,"position":4},"title":"Kate Matsudaira","author":"Viktor Sanek","date":"January 27, 2019","format":false,"excerpt":"I read an interesting article \"Design Patterns for Managing Up\" that discusses four challenging situations at work and approaches to resolve them. It turns out that the author is technologist Kate Matsudaira, previously working at Microsoft, Amazon, and several startups. Kate is currently a director of engineering at Google. Kate\u2026","rel":"","context":"In &quot;Management&quot;","block_context":{"text":"Management","link":"https:\/\/codingrestart.com\/home\/category\/management\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/codingrestart.com\/wp-content\/uploads\/2019\/01\/maxresdefault.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/codingrestart.com\/wp-content\/uploads\/2019\/01\/maxresdefault.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/codingrestart.com\/wp-content\/uploads\/2019\/01\/maxresdefault.jpg?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/codingrestart.com\/wp-content\/uploads\/2019\/01\/maxresdefault.jpg?resize=700%2C400&ssl=1 2x, https:\/\/i0.wp.com\/codingrestart.com\/wp-content\/uploads\/2019\/01\/maxresdefault.jpg?resize=1050%2C600&ssl=1 3x"},"classes":[]},{"id":88,"url":"https:\/\/codingrestart.com\/home\/webassembly\/","url_meta":{"origin":11,"position":5},"title":"WebAssembly","author":"Viktor Sanek","date":"February 3, 2019","format":false,"excerpt":"Until recently, JavaScript was the only language supported by all web browsers. But it was difficult to compile it efficiently and the JavaScript applications typically run much slower than native applications. Then, programmers from the four main browser vendors designed a new language, sort of a machine code for the\u2026","rel":"","context":"In &quot;Web&quot;","block_context":{"text":"Web","link":"https:\/\/codingrestart.com\/home\/category\/web\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/codingrestart.com\/wp-content\/uploads\/2019\/01\/WASM.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/codingrestart.com\/wp-content\/uploads\/2019\/01\/WASM.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/codingrestart.com\/wp-content\/uploads\/2019\/01\/WASM.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/codingrestart.com\/wp-content\/uploads\/2019\/01\/WASM.png?resize=700%2C400&ssl=1 2x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/codingrestart.com\/home\/wp-json\/wp\/v2\/posts\/11","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codingrestart.com\/home\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codingrestart.com\/home\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codingrestart.com\/home\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codingrestart.com\/home\/wp-json\/wp\/v2\/comments?post=11"}],"version-history":[{"count":5,"href":"https:\/\/codingrestart.com\/home\/wp-json\/wp\/v2\/posts\/11\/revisions"}],"predecessor-version":[{"id":17,"href":"https:\/\/codingrestart.com\/home\/wp-json\/wp\/v2\/posts\/11\/revisions\/17"}],"wp:attachment":[{"href":"https:\/\/codingrestart.com\/home\/wp-json\/wp\/v2\/media?parent=11"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codingrestart.com\/home\/wp-json\/wp\/v2\/categories?post=11"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codingrestart.com\/home\/wp-json\/wp\/v2\/tags?post=11"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}