{"id":1240,"date":"2013-07-18T19:15:53","date_gmt":"2013-07-18T17:15:53","guid":{"rendered":"http:\/\/foton.no-ip.com\/blog\/?p=1240"},"modified":"2013-07-18T20:25:04","modified_gmt":"2013-07-18T18:25:04","slug":"dynamic-arrays-and-the-lack-of-them-in-c","status":"publish","type":"post","link":"https:\/\/foton.szikraistvan.hu\/blog\/?p=1240","title":{"rendered":"dynamic arrays and the lack of them in C"},"content":{"rendered":"<p>I love Pascal and Delphi. In particular I love its memory management.<br \/>\nI can use string, array of byte types. They are both dynamic arrays. They are not just pointers to a memory region, they have a size or Length (how Delphi calls them).<br \/>\nThe great thing about them is that they can be re-sized. But an other great thing is that you can check their sizes no matter what (if you forgot to remember them, or if you receive them as argument of a function\/procedure\/method).<br \/>\n<a href=\"http:\/\/www.delphibasics.co.uk\/RTL.asp?Name=SetLength\">SetLength()<\/a><br \/>\n<a href=\"http:\/\/www.delphibasics.co.uk\/RTL.asp?Name=Length\">Length()<\/a><br \/>\n<a href=\"http:\/\/www.delphibasics.co.uk\/RTL.asp?Name=Low\">Low()<\/a><br \/>\n<a href=\"http:\/\/www.delphibasics.co.uk\/RTL.asp?Name=High\">High()<\/a><br \/>\n<a href=\"http:\/\/rvelthuis.de\/articles\/articles-openarr.html\">Open array parameters<\/a><br \/>\n^^^^ This is what I need in C!<\/p>\n<p>In C it&#8217;s like they want to hide this information from you. First, there is no such thing as dynamic array in C (there are only pointers). There are arrays in C++ (but I read that you should avoid them at all cost, and use vector<t> instead), but also you cannot re-size them, so they are not fixed length, but not really dynamic either.<br \/>\nSo how can you get the length of an array or memory region?<br \/>\ncommon methods:<br \/>\nIn C you can use <code>#define lengthof(array) (sizeof(array)\/sizeof(array[0]))<\/code> if it&#8217;s a fixed length array, but you cannot use it on function arguments, or pointers&#8230;<\/p>\n<p>There is a clever template (if you write it) in C++<br \/>\n<code>template<typename T, unsigned int N><br \/>\ninline unsigned int length( T(&a)[N] )  {    return N;  }<\/typename><\/code> that can be used to determine length of arrays.<\/p>\n<p>So where are the real dynamic, realizable arrays, and how to implement them?<br \/>\nIf you want to allocate memory run-time (and re-size them) you can use malloc() (realloc() and free())<br \/>\nThat&#8217;s all fine until you want to get the size of them. Which can happen if you allocate the memory outside of a function where you want to use it. There is no std libc function to get the (malloc) allocated memory size which is kept somewhere (so free() can free it, realloc can re-size it and available memory regions can be kept track of). There is however a GNU extension <strong>malloc_usable_size<\/strong>() which I, we desperately need (even if we didn&#8217;t know it).<br \/>\n.. Well we need an array whose size we can check no matter if it&#8217;s dynamic or static, but&#8230;<br \/>\nAlso note that malloc_usable_size does NOT actually return the requested size, but the allocated one (due to alignment, minimum block size), but at least we can use it for asserts.<br \/>\nSo we still need to keep track of array sizes separately (extra variables, extra arguments) and risk messing them up&#8230;<br \/>\n(or rewrite everything to use structures of size, pointer pairs)<\/p>\n<p>Another topic is multidimensional (dynamic) arrays (not just arrays of arrays)&#8230;<\/p>\n<p>Note: This actually I have written just now \ud83d\ude42 See I do not just reuse ages old materials:)<\/t><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I love Pascal and Delphi. In particular I love its memory management. I can use string, array of byte types. They are both dynamic arrays. They are not just pointers to a memory region, they have a size or Length &hellip; <a href=\"https:\/\/foton.szikraistvan.hu\/blog\/?p=1240\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"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":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[32],"tags":[135,105,70],"class_list":["post-1240","post","type-post","status-publish","format-standard","hentry","category-cooding","tag-c","tag-delphi","tag-programozas"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3E7AZ-k0","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/foton.szikraistvan.hu\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1240","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/foton.szikraistvan.hu\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/foton.szikraistvan.hu\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/foton.szikraistvan.hu\/blog\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/foton.szikraistvan.hu\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1240"}],"version-history":[{"count":9,"href":"https:\/\/foton.szikraistvan.hu\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1240\/revisions"}],"predecessor-version":[{"id":1249,"href":"https:\/\/foton.szikraistvan.hu\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1240\/revisions\/1249"}],"wp:attachment":[{"href":"https:\/\/foton.szikraistvan.hu\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1240"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/foton.szikraistvan.hu\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1240"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/foton.szikraistvan.hu\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1240"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}