{"id":5570,"date":"2023-04-11T08:48:39","date_gmt":"2023-04-11T06:48:39","guid":{"rendered":"https:\/\/www.pschatzmann.ch\/home\/?p=5570"},"modified":"2023-04-11T09:00:24","modified_gmt":"2023-04-11T07:00:24","slug":"esp32-memory-management-and-floats-with-malloc_cap_32bit","status":"publish","type":"post","link":"https:\/\/www.pschatzmann.ch\/home\/2023\/04\/11\/esp32-memory-management-and-floats-with-malloc_cap_32bit\/","title":{"rendered":"ESP32 Memory Management: floats and MALLOC_CAP_32BIT"},"content":{"rendered":"<p>We can dynamically allocate memory with malloc() or use <a href=\"https:\/\/docs.espressif.com\/projects\/esp-idf\/en\/latest\/esp32\/api-reference\/system\/mem_alloc.html\">the dedicated ESP32 specific functions<\/a>. In general we need to use byte aligned allocations (MALLOC_CAP_8BIT).<\/p>\n<p>When we have used up all available memory space, we still have <strong>4 byte aligned memory available<\/strong> that we can use to store arrays of <strong>int32_t data<\/strong>. Unfortunately we can not use this to store floats. Here is the info from the documentation:<\/p>\n<p><em><q>Please note that on ESP32 series chips, MALLOC_CAP_32BIT cannot be used for storing floating-point variables. This is because MALLOC_CAP_32BIT may return instruction RAM, and the floating-point assembly instructions on ESP32 cannot access instruction RAM<\/q><\/em><\/p>\n<p>The following code leads to a runtime exception:<\/p>\n<pre><code>float *data = nullptr;\nconst int count = 1000;\n\nvoid setup() {\n  data = (float*) heap_caps_malloc(count*sizeof(float), MALLOC_CAP_32BIT);\n  assert(data!=nullptr);\n}\n\nvoid loop() {\n  for (int j=0;j&lt;count;j++){\n    data[j]=0.1+j; \/\/ assign float value\n    println(data[j]);\n  }\n}\n<\/code><\/pre>\n<p>But we can cheat and <strong>internally store the 4 float bytes as uint32_t<\/strong> and still provide floating point operations: C++ is cool and can help us to hide this complexity, so I created the <a href=\"https:\/\/github.com\/pschatzmann\/arduino-audio-tools\/blob\/main\/src\/AudioBasic\/Float32.h\">float32 class<\/a> that does exactly this.<\/p>\n<p>The following sketch will work without any problems:<\/p>\n<pre><code>float32 *data = nullptr;\nconst int count = 1000;\n\nvoid setup() {\n  data = (float32*) heap_caps_malloc(count*sizeof(float), MALLOC_CAP_32BIT);\n  assert(data!=nullptr);\n}\n\nvoid loop() {\n  for (int j=0;j&lt;count;j++){\n    data[j]=0.1+j;\n    println(data[j]);\n  }\n}\n<\/code><\/pre>\n<p>With the help of this <strong>hack<\/strong> we can use the full available memory of the ESP32 and not only the memory which is byte aligned!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We can dynamically allocate memory with malloc() or use the dedicated ESP32 specific functions. In general we need to use byte aligned allocations (MALLOC_CAP_8BIT). When we have used up all available memory space, we still have 4 byte aligned memory available that we can use to store arrays of int32_t [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[20,22],"tags":[],"class_list":["post-5570","post","type-post","status-publish","format-standard","hentry","category-arduino","category-machine-sound"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>ESP32 Memory Management: floats and MALLOC_CAP_32BIT - Phil Schatzmann<\/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:\/\/www.pschatzmann.ch\/home\/2023\/04\/11\/esp32-memory-management-and-floats-with-malloc_cap_32bit\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"ESP32 Memory Management: floats and MALLOC_CAP_32BIT - Phil Schatzmann\" \/>\n<meta property=\"og:description\" content=\"We can dynamically allocate memory with malloc() or use the dedicated ESP32 specific functions. In general we need to use byte aligned allocations (MALLOC_CAP_8BIT). When we have used up all available memory space, we still have 4 byte aligned memory available that we can use to store arrays of int32_t [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pschatzmann.ch\/home\/2023\/04\/11\/esp32-memory-management-and-floats-with-malloc_cap_32bit\/\" \/>\n<meta property=\"og:site_name\" content=\"Phil Schatzmann\" \/>\n<meta property=\"article:published_time\" content=\"2023-04-11T06:48:39+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-04-11T07:00:24+00:00\" \/>\n<meta name=\"author\" content=\"pschatzmann\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"pschatzmann\" \/>\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:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2023\\\/04\\\/11\\\/esp32-memory-management-and-floats-with-malloc_cap_32bit\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2023\\\/04\\\/11\\\/esp32-memory-management-and-floats-with-malloc_cap_32bit\\\/\"},\"author\":{\"name\":\"pschatzmann\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/#\\\/schema\\\/person\\\/73a53638a4e34e8373405fd737dac9b1\"},\"headline\":\"ESP32 Memory Management: floats and MALLOC_CAP_32BIT\",\"datePublished\":\"2023-04-11T06:48:39+00:00\",\"dateModified\":\"2023-04-11T07:00:24+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2023\\\/04\\\/11\\\/esp32-memory-management-and-floats-with-malloc_cap_32bit\\\/\"},\"wordCount\":196,\"commentCount\":1,\"publisher\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/#\\\/schema\\\/person\\\/73a53638a4e34e8373405fd737dac9b1\"},\"articleSection\":[\"Arduino\",\"Machine Sound\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2023\\\/04\\\/11\\\/esp32-memory-management-and-floats-with-malloc_cap_32bit\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2023\\\/04\\\/11\\\/esp32-memory-management-and-floats-with-malloc_cap_32bit\\\/\",\"url\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2023\\\/04\\\/11\\\/esp32-memory-management-and-floats-with-malloc_cap_32bit\\\/\",\"name\":\"ESP32 Memory Management: floats and MALLOC_CAP_32BIT - Phil Schatzmann\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/#website\"},\"datePublished\":\"2023-04-11T06:48:39+00:00\",\"dateModified\":\"2023-04-11T07:00:24+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2023\\\/04\\\/11\\\/esp32-memory-management-and-floats-with-malloc_cap_32bit\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2023\\\/04\\\/11\\\/esp32-memory-management-and-floats-with-malloc_cap_32bit\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2023\\\/04\\\/11\\\/esp32-memory-management-and-floats-with-malloc_cap_32bit\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"ESP32 Memory Management: floats and MALLOC_CAP_32BIT\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/#website\",\"url\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/\",\"name\":\"Phil Schatzmann Consulting\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/#\\\/schema\\\/person\\\/73a53638a4e34e8373405fd737dac9b1\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/#\\\/schema\\\/person\\\/73a53638a4e34e8373405fd737dac9b1\",\"name\":\"pschatzmann\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/wp-content\\\/uploads\\\/2022\\\/08\\\/pschatzmann.png\",\"url\":\"https:\\\/\\\/www.pschatzmann.ch\\\/wp-content\\\/uploads\\\/2022\\\/08\\\/pschatzmann.png\",\"contentUrl\":\"https:\\\/\\\/www.pschatzmann.ch\\\/wp-content\\\/uploads\\\/2022\\\/08\\\/pschatzmann.png\",\"width\":305,\"height\":305,\"caption\":\"pschatzmann\"},\"logo\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/wp-content\\\/uploads\\\/2022\\\/08\\\/pschatzmann.png\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"ESP32 Memory Management: floats and MALLOC_CAP_32BIT - Phil Schatzmann","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:\/\/www.pschatzmann.ch\/home\/2023\/04\/11\/esp32-memory-management-and-floats-with-malloc_cap_32bit\/","og_locale":"en_US","og_type":"article","og_title":"ESP32 Memory Management: floats and MALLOC_CAP_32BIT - Phil Schatzmann","og_description":"We can dynamically allocate memory with malloc() or use the dedicated ESP32 specific functions. In general we need to use byte aligned allocations (MALLOC_CAP_8BIT). When we have used up all available memory space, we still have 4 byte aligned memory available that we can use to store arrays of int32_t [&hellip;]","og_url":"https:\/\/www.pschatzmann.ch\/home\/2023\/04\/11\/esp32-memory-management-and-floats-with-malloc_cap_32bit\/","og_site_name":"Phil Schatzmann","article_published_time":"2023-04-11T06:48:39+00:00","article_modified_time":"2023-04-11T07:00:24+00:00","author":"pschatzmann","twitter_card":"summary_large_image","twitter_misc":{"Written by":"pschatzmann","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.pschatzmann.ch\/home\/2023\/04\/11\/esp32-memory-management-and-floats-with-malloc_cap_32bit\/#article","isPartOf":{"@id":"https:\/\/www.pschatzmann.ch\/home\/2023\/04\/11\/esp32-memory-management-and-floats-with-malloc_cap_32bit\/"},"author":{"name":"pschatzmann","@id":"https:\/\/www.pschatzmann.ch\/home\/#\/schema\/person\/73a53638a4e34e8373405fd737dac9b1"},"headline":"ESP32 Memory Management: floats and MALLOC_CAP_32BIT","datePublished":"2023-04-11T06:48:39+00:00","dateModified":"2023-04-11T07:00:24+00:00","mainEntityOfPage":{"@id":"https:\/\/www.pschatzmann.ch\/home\/2023\/04\/11\/esp32-memory-management-and-floats-with-malloc_cap_32bit\/"},"wordCount":196,"commentCount":1,"publisher":{"@id":"https:\/\/www.pschatzmann.ch\/home\/#\/schema\/person\/73a53638a4e34e8373405fd737dac9b1"},"articleSection":["Arduino","Machine Sound"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.pschatzmann.ch\/home\/2023\/04\/11\/esp32-memory-management-and-floats-with-malloc_cap_32bit\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.pschatzmann.ch\/home\/2023\/04\/11\/esp32-memory-management-and-floats-with-malloc_cap_32bit\/","url":"https:\/\/www.pschatzmann.ch\/home\/2023\/04\/11\/esp32-memory-management-and-floats-with-malloc_cap_32bit\/","name":"ESP32 Memory Management: floats and MALLOC_CAP_32BIT - Phil Schatzmann","isPartOf":{"@id":"https:\/\/www.pschatzmann.ch\/home\/#website"},"datePublished":"2023-04-11T06:48:39+00:00","dateModified":"2023-04-11T07:00:24+00:00","breadcrumb":{"@id":"https:\/\/www.pschatzmann.ch\/home\/2023\/04\/11\/esp32-memory-management-and-floats-with-malloc_cap_32bit\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pschatzmann.ch\/home\/2023\/04\/11\/esp32-memory-management-and-floats-with-malloc_cap_32bit\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.pschatzmann.ch\/home\/2023\/04\/11\/esp32-memory-management-and-floats-with-malloc_cap_32bit\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pschatzmann.ch\/home\/"},{"@type":"ListItem","position":2,"name":"ESP32 Memory Management: floats and MALLOC_CAP_32BIT"}]},{"@type":"WebSite","@id":"https:\/\/www.pschatzmann.ch\/home\/#website","url":"https:\/\/www.pschatzmann.ch\/home\/","name":"Phil Schatzmann Consulting","description":"","publisher":{"@id":"https:\/\/www.pschatzmann.ch\/home\/#\/schema\/person\/73a53638a4e34e8373405fd737dac9b1"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.pschatzmann.ch\/home\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/www.pschatzmann.ch\/home\/#\/schema\/person\/73a53638a4e34e8373405fd737dac9b1","name":"pschatzmann","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2022\/08\/pschatzmann.png","url":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2022\/08\/pschatzmann.png","contentUrl":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2022\/08\/pschatzmann.png","width":305,"height":305,"caption":"pschatzmann"},"logo":{"@id":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2022\/08\/pschatzmann.png"}}]}},"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/posts\/5570","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/comments?post=5570"}],"version-history":[{"count":18,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/posts\/5570\/revisions"}],"predecessor-version":[{"id":5588,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/posts\/5570\/revisions\/5588"}],"wp:attachment":[{"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/media?parent=5570"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/categories?post=5570"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/tags?post=5570"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}