{"id":21923,"date":"2022-11-16T19:04:32","date_gmt":"2022-11-16T13:34:32","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-c-memory-allocationd-closed\/"},"modified":"2022-11-16T19:04:32","modified_gmt":"2022-11-16T13:34:32","slug":"solved-c-memory-allocationd-closed","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-c-memory-allocationd-closed\/","title":{"rendered":"[Solved] C memory allocationd [closed]"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-64091098\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"64091098\" data-parentid=\"64090033\" data-score=\"2\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<blockquote>\n<p>I want to have a array of struct A, so I declared the array in function &#8216;mem_init&#8217;.<\/p>\n<\/blockquote>\n<p>this is not what you did because <code>struct A *arr_A[n];<\/code> is an array of <em>pointers<\/em> to<code>struct A<\/code>.<\/p>\n<p>An array of <code>struct A<\/code> can be<\/p>\n<pre><code>struct A arr_A[n]; \/\/ variable length array \n<\/code><\/pre>\n<p>or<\/p>\n<pre><code>struct A * arr_A = malloc(n * sizeof(struct A));\n<\/code><\/pre>\n<p>Of course having an array of <em>struct A<\/em> rather that pointer to the loop is modified :<\/p>\n<pre><code>for(int i=0; i&lt;n; i++){\n   arr_A[i].thing1 =1;\n   arr_A[i].thing2 =2;\n}\n<\/code><\/pre>\n<blockquote>\n<p>now I want to make a function to free the memory of &#8216;arr_A&#8217;<\/p>\n<\/blockquote>\n<p>if <em>arr_A<\/em> is your array of pointer you can do (supposing the definition of <em>A<\/em> known out of <em>mem_init<\/em>) :<\/p>\n<pre><code>void mem_free(struct A ** arr_A, int n)\n{\n   for(int i=0; i&lt;n; i++)\n     free(arr_A[i]);\n}\n<\/code><\/pre>\n<p>else (the function is not really useful and <em>free<\/em> can be used directly rather than to define and call <em>mem_free<\/em>) :<\/p>\n<pre><code>void mem_free(struct A *arr_A)\n{\n   free(arr_A);\n}\n<\/code><\/pre>\n<hr>\n<p>Out of that currently the call of <em>mem_free<\/em> can only be done in <em>mem_init<\/em> because only it knows the array, do you really want that ? If not <em>mem_init<\/em> can return the address of the array, using your array of pointers a solution is :<\/p>\n<pre><code>#include &lt;stdlib.h&gt;\n\nstruct A {\n  int thing1;\n  double thing2;\n};\n\nstruct A ** mem_init(int n) {\n  struct A ** arr_A = malloc(n * sizeof(struct A *));\n  \n  for(int i=0; i&lt;n; i++){\n    arr_A[i] = malloc(sizeof(struct A));\n    arr_A[i]-&gt;thing1 =1;\n    arr_A[i]-&gt;thing2 =2;\n  }\n  \n  return arr_A;\n}\n\nvoid mem_free(struct A ** arr_A, int n)\n{\n  for(int i=0; i&lt;n; i++)\n    free(arr_A[i]);\n  free(arr_A);\n}\n\nint main()\n{\n  const int nelt = 10;\n  struct A ** arr_A = mem_init(nelt);\n  \n  mem_free(arr_A, nelt);\n  return 0;\n}\n<\/code><\/pre>\n<p>Note I moved the definition of <em>A<\/em> outside, and the array is not anymore a local VLA to <em>mem_init<\/em> to be able to use it after the execution of <em>mem_init<\/em><\/p>\n<p>Compilation and execution:<\/p>\n<pre><code>pi@raspberrypi:\/tmp $ gcc -Wall c.c\npi@raspberrypi:\/tmp $ .\/a.out\npi@raspberrypi:\/tmp $ valgrind .\/a.out\n==12721== Memcheck, a memory error detector\n==12721== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.\n==12721== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info\n==12721== Command: .\/a.out\n==12721== \n==12721== \n==12721== HEAP SUMMARY:\n==12721==     in use at exit: 0 bytes in 0 blocks\n==12721==   total heap usage: 11 allocs, 11 frees, 200 bytes allocated\n==12721== \n==12721== All heap blocks were freed -- no leaks are possible\n==12721== \n==12721== For lists of detected and suppressed errors, rerun with: -s\n==12721== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)\npi@raspberrypi:\/tmp $ \n<\/code><\/pre>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\"><\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved C memory allocationd [closed] <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] I want to have a array of struct A, so I declared the array in function &#8216;mem_init&#8217;. this is not what you did because struct A *arr_A[n]; is an array of pointers tostruct A. An array of struct A can be struct A arr_A[n]; \/\/ variable length array or struct A * arr_A = &#8230; <a title=\"[Solved] C memory allocationd [closed]\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-c-memory-allocationd-closed\/\" aria-label=\"More on [Solved] C memory allocationd [closed]\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[324,4961,377],"class_list":["post-21923","post","type-post","status-publish","format-standard","hentry","category-solved","tag-c","tag-clion","tag-dynamic-memory-allocation"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] C memory allocationd [closed] - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-c-memory-allocationd-closed\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] C memory allocationd [closed] - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] I want to have a array of struct A, so I declared the array in function &#8216;mem_init&#8217;. this is not what you did because struct A *arr_A[n]; is an array of pointers tostruct A. An array of struct A can be struct A arr_A[n]; \/\/ variable length array or struct A * arr_A = ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-c-memory-allocationd-closed\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-11-16T13:34:32+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-c-memory-allocationd-closed\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-c-memory-allocationd-closed\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] C memory allocationd [closed]\",\"datePublished\":\"2022-11-16T13:34:32+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-c-memory-allocationd-closed\/\"},\"wordCount\":202,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"c++\",\"clion\",\"dynamic-memory-allocation\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-c-memory-allocationd-closed\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-c-memory-allocationd-closed\/\",\"name\":\"[Solved] C memory allocationd [closed] - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-11-16T13:34:32+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-c-memory-allocationd-closed\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-c-memory-allocationd-closed\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-c-memory-allocationd-closed\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] C memory allocationd [closed]\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] C memory allocationd [closed] - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-c-memory-allocationd-closed\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] C memory allocationd [closed] - JassWeb","og_description":"[ad_1] I want to have a array of struct A, so I declared the array in function &#8216;mem_init&#8217;. this is not what you did because struct A *arr_A[n]; is an array of pointers tostruct A. An array of struct A can be struct A arr_A[n]; \/\/ variable length array or struct A * arr_A = ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-c-memory-allocationd-closed\/","og_site_name":"JassWeb","article_published_time":"2022-11-16T13:34:32+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-c-memory-allocationd-closed\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-c-memory-allocationd-closed\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] C memory allocationd [closed]","datePublished":"2022-11-16T13:34:32+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-c-memory-allocationd-closed\/"},"wordCount":202,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["c++","clion","dynamic-memory-allocation"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-c-memory-allocationd-closed\/","url":"https:\/\/jassweb.com\/solved\/solved-c-memory-allocationd-closed\/","name":"[Solved] C memory allocationd [closed] - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-11-16T13:34:32+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-c-memory-allocationd-closed\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-c-memory-allocationd-closed\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-c-memory-allocationd-closed\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] C memory allocationd [closed]"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/21923","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=21923"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/21923\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=21923"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=21923"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=21923"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}