MALLOC_CAP_32BIT and float

Posts: 2
Joined: Fri Mar 23, 2018 12:33 pm

MALLOC_CAP_32BIT and float

Postby nitski » Fri Mar 23, 2018 12:42 pm

Hello all,

Is float type capable for MALLOC_CAP_32BIT ?

I used simple the code :

Code: Select all

float *test=(float *) heap_caps_malloc(sizeof(float)*10,MALLOC_CAP_32BIT);
It causes to LoadStoreError. With int it works fine...

User avatar
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: MALLOC_CAP_32BIT and float

Postby kolban » Fri Mar 23, 2018 3:21 pm

Ive been horribly wrong in a lot of my posts recently but, undeterred in flaunting my ignorance ... my answer would be "sure that works fine". My understanding is that MALLOC_CAP_32BIT allocates storage from the heap aligned to 32bit boundaries. For example, if one called it 5 times allocating 1 byte, then the 20 bytes would be taken from the heap.

We would want to look and see how many bytes a float would be ... but I'm going to guess 32 or 64 bits.

The first test I can think of is to examine the return value from heap_caps_malloc(). Make sure it isn't NULL. If (for some reason) you asked for 80 bytes (sizeof(float) * 10 = 8 * 10 = 80) and there wasn't 80 bytes of contiguous storage available then you would get NULL and an attempt to write into NULL[0] would fail.
Free book on ESP32 available here:

Posts: 2679
Joined: Thu Nov 26, 2015 4:08 am

Re: MALLOC_CAP_32BIT and float

Postby ESP_Sprite » Sat Mar 24, 2018 2:00 am

I think it's not capable of doing that. The MALLOC_CAP_32BIT is a bit misleading: the actual reality is that only L32I/S32I instructions can access this memory to retrieve data, and those instructions are only capable of loading integer values.

Who is online

Users browsing this forum: Baidu [Spider], Bing [Bot], ESP_Dazz and 22 guests