Malloc() in loop()

If I use a malloc() in the loop(), what actually happens? Does it try to allocate an array of memory every time the loop is executed? Or is it smart enough to know that the memory has been allocated and no need to allocate more? Or, is it a bad idea to use malloc() in Arduino environment and I should simply declare a fixed number of bytes (or int or float ...)?

Yes. You can use free at the end of the function to free the allocated memory though.

That would indeed be preferable.

It does exactly what the code tells it to do, which might or might not be what you want done.

1 Like

if this is enough for your need then it's generally preferable indeed. This way you know for sure the bytes are pre allocated and you won't run out of memory at run time because of this.

On the smallest Arduinos, it's risky. For a program that runs on a PC, it makes sense to use memory allocation so that it can be a good neighbor to the many other processes that need RAM too. For most Arduinos, there are no other processes to share it with.

Also, on a PC, if allocation fails, a sensible message and graceful degradation can occur. An Arduino program is more likely to silently crash or misbehave in a similar situation, particularly if it has no user interface.

I am using ESP-32. If I simply declare another array as "float samp[16384]", it won't compile and says something about `dram0_0_seg' overflowed by 60840 bytes.
From reading the ESP forum, it was said that the static global variable is limited to about 100K which I already used most of, but the other 220K can be used with malloc(). Well, I tried it with "float *samp" as global, and in setup(), I use "samp = (float *) malloc(65536);" This didn't seem to work and crashed when the array is being used.

I tried to compile this on an ESP32

float samp[16384];

void setup() {
  Serial.begin(115200); Serial.println();
}

void loop() {
  for (int i = 0; i < 16384; i++) {
    samp[i] = random(0, i);
    Serial.println(samp[i]);
  }
}

there was no compilation error

please post sample code demonstrating your issue. (remember the stack size is limited on an ESP32 within the Arduino environment)

You could spin up a FreeRTOS task with a larger stack than the one allocated to the task that handles setup() / loop(). There is an upper limit out there somewhere, but I haven't explored it.

indeed - that was just a reminder for the OP if he was allocating large arrays of non static data in various functions

Thanks for trying but my code has already used short data[8192], COMPLEX fft_array[8192], and etc before I tried to add another samp[16384]. On top of that, WiFi function used some more memory. All of these resulted in overflow of this one segment of memory.

I have now avoided the problem by reusing the existing arrays.

Forums are dangerous places to learn; I prefer the docs:
Memory Types - ESP32-S2 - β€” ESP-IDF Programming Guide latest documentation (espressif.com)

Thus, you must tell the source code that you intend on using.

Alternatively, it’s possible to specify IRAM placement in the source code using the IRAM_ATTR macro:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.