{"id":5475,"date":"2014-03-30T22:28:03","date_gmt":"2014-03-30T22:28:03","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/freeing-vfree-ing-pointer-to-volatile-data-collection-of-common-programming-errors\/"},"modified":"2014-03-30T22:28:03","modified_gmt":"2014-03-30T22:28:03","slug":"freeing-vfree-ing-pointer-to-volatile-data-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/freeing-vfree-ing-pointer-to-volatile-data-collection-of-common-programming-errors\/","title":{"rendered":"Freeing (vfree-ing) pointer to volatile data-Collection of common programming errors"},"content":{"rendered":"<p><code>volatile<\/code> seems to be a never ending question of every one. I thought I knew everything about it, but then I encountered this:<\/p>\n<p>So, I have a piece of memory shared between threads and I defined it like this:<\/p>\n<pre><code>volatile type *name;\n<\/code><\/pre>\n<p>If it makes you feel better, you can imagine <code>type<\/code> is just an <code>int<\/code>.<\/p>\n<p>This means I have a pointer (that is not volatile) to some data that are volatile. So, for example when it comes to optimizing, the compiler can cache the value of <code>name<\/code> but not <code>name[0]<\/code>. Am I right?<\/p>\n<p>So, now I am <code>vfree<\/code>ing this pointer (it&#8217;s in a Linux kernel module) and it tells me that <code>vfree<\/code> expects <code>const void *<\/code> while I am passing it <code>volatile type *<\/code>.<\/p>\n<p>I understand how it can be dangerous to pass a <code>volatile type *<\/code> as a <code>type *<\/code> because in that function, the values of <code>name[i]<\/code> could be cached (as a result of optimization) which is not desirable.<\/p>\n<p>I don&#8217;t understand why though, <code>vfree<\/code> expects me to send it a pointer necessarily to non-volatile data. Is there something I am missing there? Or is it just the guys who wrote <code>vfree<\/code> not thinking about this situation?<\/p>\n<p>I assume me simply casting my pointer to <code>void *<\/code> would not cause any harm, is that right?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>volatile seems to be a never ending question of every one. I thought I knew everything about it, but then I encountered this: So, I have a piece of memory shared between threads and I defined it like this: volatile type *name; If it makes you feel better, you can imagine type is just an [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-5475","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5475","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=5475"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5475\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=5475"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=5475"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=5475"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}