{"id":1147,"date":"2022-08-30T15:13:10","date_gmt":"2022-08-30T15:13:10","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/free-delete-union-malloc-new-array-in-c-c-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:13:10","modified_gmt":"2022-08-30T15:13:10","slug":"free-delete-union-malloc-new-array-in-c-c-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/free-delete-union-malloc-new-array-in-c-c-collection-of-common-programming-errors\/","title":{"rendered":"free\/delete union malloc\/new Array in C\/C++-Collection of common programming errors"},"content":{"rendered":"<p>It has nothing to do with the malloc() implementation. The union in your example uses the same memory location to store one of three &#8220;different&#8221; pointers. However, all pointers, no matter what they point to, are the same size &#8211; which is the native integer size of the architecture you&#8217;re on &#8211; 32 bits on 32-bit systems and 64-bits on 64-bit systems, etc. This is because a pointer is an address in memory, which may be represented by an integer.<\/p>\n<p>Let&#8217;s say your arr is located at address 0x10000 (the pointer to your pointer, if you will.) Let&#8217;s say malloc() finds you a memory location at 0x666660. You assign arr.array_f to this value &#8211; which means you store the pointer 0x666660 in the location 0x10000. Then you write your floats into 0x666660 to 0x666688.<\/p>\n<p>Now, you attempt to access arr.array_i. Because you&#8217;re using a union, the address of arr.array_i is the same as the address of arr.array_f and arr.array_c. So you are reading from the address 0x10000 again &#8211; and you read out the pointer 0x666660. Since this is the same pointer malloc returned earlier, you can go ahead and free it.<\/p>\n<p>That said, attempting to interpret integers as text, or floating point numbers as integers, etc, will clearly lead to ruin. If arr.array_i[0] == 1, then arr.array_f[0] will definitely not == 1 and arr.array_c[0] will have no bearing on the character &#8216;1&#8217;. You can try &#8220;viewing&#8221; memory this way as an exercise (loop and printf()) &#8211; but you won&#8217;t achieve anything.<\/p>\n<p id=\"rop\"><small>Originally posted 2013-11-09 23:32:21. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>It has nothing to do with the malloc() implementation. The union in your example uses the same memory location to store one of three &#8220;different&#8221; pointers. However, all pointers, no matter what they point to, are the same size &#8211; which is the native integer size of the architecture you&#8217;re on &#8211; 32 bits on [&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-1147","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1147","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=1147"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1147\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1147"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1147"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1147"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}