# Malloc/free issues

**URL:** <https://forum.arduino.cc/t/malloc-free-issues/1397078>\
**Category:** Programming\
**Created:** [July 21, 2025, 9:57pm UTC](https://forum.arduino.cc/t/malloc-free-issues/1397078 "2025-07-21T21:57:14Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![lapowels](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/lapowels/32/1126500_2.png) [@lapowels](https://forum.arduino.cc/u/lapowels)\
**Post date:** [July 21, 2025, 9:57pm UTC](https://forum.arduino.cc/t/malloc-free-issues/1397078/1 "2025-07-21T21:57:14Z")

</div>

I am using the Arduino GIGA R1 and am having issues with alloc and free. In the following code snippet I am trying to verify that my memory allocation of SDRAM stays within the true memory available. It is: SDRAM\_START\_ADDRESS and SDRAM\_END\_ADDRESS which are defined in the SDRAM library.

```arduino
  c1 = (unsigned char*)SDRAM.malloc(1);
  // There is a 8 Bbye malloc overhead added to the byte size requested.
  EndingAddress = (void*) (c1 + RequestedSize + 8 );
  SDRAM.free(c1);  
  if ((int)EndingAddress < SDRAM_END_ADDRESS) {
    RequestedMemoryPtrs.pHead = SDRAM.malloc(RequestedSize);

```

When the SDRAM.free(c1) is executed the system crashes and the red light on the back of the Arduino GIGA R1 starts flashing. If I comment out

` SDRAM.free(c1);`

My program works perfectly in writing to the SDRAM area. I can live with the few wasted bytes but i am wondering if there is another way to free SDRAM?

Thanks

---

<div class="post-metadata">

**Author:** ![kenb4](https://avatars.discourse-cdn.com/v4/letter/k/bb73d2/32.png) [@kenb4](https://forum.arduino.cc/u/kenb4)\
**Post date:** [July 21, 2025, 10:19pm UTC](https://forum.arduino.cc/t/malloc-free-issues/1397078/2 "2025-07-21T22:19:00Z")

</div>

Does the [`nonFrameBuffer` example](https://github.com/arduino/ArduinoCore-mbed/blob/4.3.1/libraries/Portenta_SDRAM/examples/SDRAM_operations/SDRAM_operations.ino#L15) also crash with `free`?

---

<div class="post-metadata">

**Author:** ![embeddedkiddie](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/embeddedkiddie/32/1081520_2.png) [@embeddedkiddie](https://forum.arduino.cc/u/embeddedkiddie)\
**Post date:** [July 21, 2025, 10:30pm UTC](https://forum.arduino.cc/t/malloc-free-issues/1397078/3 "2025-07-21T22:30:21Z")

</div>

I don't know much about GIGA or embed, but most MCUs allocate based on the memory alignment boundary.

> [@lapowels](#):
>
> ```arduino
> c1 = (unsigned char*)SDRAM.malloc(1);
> 
> ```

In this code, `c1` is likely to be `NULL`, so I think you should check it as follows.

```arduino
  c1 = (unsigned char*)SDRAM.malloc(1);
  if (c1 == NULL) {
    // some error trapping code
  }

```

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [July 22, 2025, 6:55am UTC](https://forum.arduino.cc/t/malloc-free-issues/1397078/4 "2025-07-22T06:55:55Z")

</div>

I never used that board but normally, malloc allocators place control structures before the returned pointer (e.g., `ptr = block + sizeof(Header)`) and you should not assume how much overhead the allocator requires or try to allocate memory.

You should trust that SDRAM.begin() initializes the allocator with the SDRAM start and end addresses and will play nice within that space.

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [July 22, 2025, 7:03am UTC](https://forum.arduino.cc/t/malloc-free-issues/1397078/5 "2025-07-22T07:03:28Z")

</div>

As a side note

> [@lapowels](#):
>
> ```arduino
> EndingAddress = (void*) (c1 + RequestedSize + 8 );
> SDRAM.free(c1);  
> if ((int)EndingAddress < SDRAM_END_ADDRESS) {
> 
> ```

The code is possibly wrong because it performs pointer arithmetic beyond the allocated memory (c1 + RequestedSize + 8), which is undefined behavior in C++. Once in UB territory all bets are off.

Also casting that pointer to int risks truncation or sign errors on a 32-bit system, making the comparison with SDRAM\_END\_ADDRESS likely invalid.

---

<div class="post-metadata">

**Author:** ![lapowels](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/lapowels/32/1126500_2.png) [@lapowels](https://forum.arduino.cc/u/lapowels)\
**Post date:** [July 23, 2025, 1:58pm UTC](https://forum.arduino.cc/t/malloc-free-issues/1397078/6 "2025-07-23T13:58:12Z")

</div>

Yes the example program: Examples / Portenta\_SDRAM / SDRAM\_operations does run correctly. The one noticable difference between the verbose compiler outputs between my project and the example project is the following warning:

WARNING: library ArduinoGraphics claims to run on samd, renesas\_uno architecture(s) and may be incompatible with your current board which runs on mbed\_giga architecture(s).

is this an issue? how would I fix it?

Thanks

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [July 23, 2025, 4:11pm UTC](https://forum.arduino.cc/t/malloc-free-issues/1397078/7 "2025-07-23T16:11:14Z")

</div>

> [@lapowels](#):
>
> is this an issue? how would I fix it?

It means the library has not been tested against that core so it might or might not work...

if it compiles without other warnings, (assuming you activated the verbose mode), then it's a good sign but there still might be quirks...

---

<div class="post-metadata">

**Author:** ![lapowels](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/lapowels/32/1126500_2.png) [@lapowels](https://forum.arduino.cc/u/lapowels)\
**Post date:** [July 25, 2025, 5:13pm UTC](https://forum.arduino.cc/t/malloc-free-issues/1397078/8 "2025-07-25T17:13:09Z")

</div>

Found out what was causing my board to crash. First I started with the Basic Example "ArduinoLogo" and added the SDRAM functionality. This did not crash my board. See the following:

```arduino
/*
  ArduinoLogo

  created 17 Apr 2023
  by Leonardo Cavagnis
*/

#include "Arduino_H7_Video.h"
#include "lvgl.h"
#include "ArduinoGraphics.h"
#include <SDRAM.h>

#include "img_arduinologo.h"
// Alternatively, any raw RGB565 image can be included on demand using this macro
// Online image converter: https://lvgl.io/tools/imageconverter (Output format: Binary RGB565)
/*
#define INCBIN_PREFIX
#include "incbin.h"
INCBIN(test, "/home/user/Downloads/test.bin");
*/

Arduino_H7_Video Display(800, 480, GigaDisplayShield);
//Arduino_H7_Video Display(1024, 768, USBCVideo);

Image img_arduinologo(ENCODING_RGB16, (uint8_t *) texture_raw, 300, 300);

void nonFrameBuffer() {
    // Initilize SDRAM for non-framebuffer operations
// SDRAM.begin(); // is the same as SDRAM.begin(SDRAM_START_ADDRESS);

    // Now we can malloc() and free() in the whole RAM space
    // For example, let's create a 7MB array
    uint8_t* myVeryBigArray = (uint8_t*)SDRAM.malloc(1024);
    if (myVeryBigArray ==0) {
      Serial.println("SDRAM myVeryBigArray did not allocate");
    } else {
      Serial.println("SDRAM myVeryBigArray allocated");
    }
    // and a small one
    uint8_t* mySmallArray = (uint8_t*)SDRAM.malloc(128);
    if (mySmallArray ==0) {
      Serial.println("SDRAM mySmallArray did not allocate");
    } else {
      Serial.println("SDRAM mySmallArray allocated");
    }

    // and use then as usual
    for (int i = 0; i<128; i++) {
        myVeryBigArray[i] = i;
        mySmallArray[i] = i*2;
    }

    // free the memory when you don't need them anymore
    Serial.println("SDRAM deallocating");
    SDRAM.free(myVeryBigArray);
    SDRAM.free(mySmallArray);
}

void setup() {
  Serial.begin(115200);
  while (!Serial);  

  Display.begin();

  Display.beginDraw();
  Display.image(img_arduinologo, (Display.width() - img_arduinologo.width())/2, (Display.height() - img_arduinologo.height())/2);
  Display.endDraw();
     nonFrameBuffer();
     Serial.println("Back from NonFrameBuffer");
}

void loop() { }

```

After much dissection of my program it came down to the following statement:

SDRAM.begin();

This statement was in my program and not in the working above test program. I removed the SDRAM.begin from my main program and it now does not crash.

My question is: What does the SDRAM.begin function do and what does it do if it is not there? If I put in the above test program, it does not crash it.

Thanks

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [July 25, 2025, 7:02pm UTC](https://forum.arduino.cc/t/malloc-free-issues/1397078/9 "2025-07-25T19:02:49Z")

</div>

I don’t know the platform in details - but source code is available

> <https://github.com/arduino/ArduinoCore-mbed/blob/cd9d7fcabf196ef2d9aa10e53efecbb0ca3f63f6/libraries/Portenta_SDRAM/src/SDRAM.cpp#L31-58>

The GIGA R1 has 1 MB of SRAM that is internal to the processor, and 8MB of SDRAM.

I would assume that if you don’t call begin() you end up playing only within the 1MB space.

---

<div class="post-metadata">

**Author:** ![lapowels](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/lapowels/32/1126500_2.png) [@lapowels](https://forum.arduino.cc/u/lapowels)\
**Post date:** [July 26, 2025, 3:11pm UTC](https://forum.arduino.cc/t/malloc-free-issues/1397078/10 "2025-07-26T15:11:48Z")

</div>

Thank you that is very helpful. I spent the night stewing about the SDRAM.begin() issue and I decided to do more testing and then I update my test program (provided in previous message). The Following was added to more represent my full sketch.

```arduino
  SDRAM.begin();

  Display.begin();
  Display.clear();
// Display.noStroke(); <<--- Offending line
  Display.rect(0, 0, Display.width(), Display.height());
  Display.fill(255, 255, 255);
  Display.endDraw();
  Display.beginDraw();
  Display.image(img_arduinologo, (Display.width() - img_arduinologo.width())/2, (Display.height() - img_arduinologo.height())/2);
  Display.endDraw();

  nonFrameBuffer();
  Serial.println("Back from NonFrameBuffer");

```

I am trying to clear the screen with a white background before displaying the startup logo. when the Dispaly.noStroke() statement is commented out the function call to NonFrameBuffer does return and the message is printed. If that statement is not commented out the board crashes. Is there a guide to help me write stuff to the GIGA display correctly. I went through

[https://docs-content.arduino.cc/tutorials/giga-display-shield/lvgl-guide/](https://docs-content.arduino.cc/tutorials/giga-display-shield/lvgl-guide/)

and did not see how to clear the screen and display a graphic.

thanks

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [July 27, 2025, 7:18am UTC](https://forum.arduino.cc/t/malloc-free-issues/1397078/11 "2025-07-27T07:18:27Z")

</div>

Back to

> [@lapowels](#):
>
> WARNING: library ArduinoGraphics claims to run on samd, renesas\_uno architecture(s) and may be incompatible with your current board which runs on mbed\_giga architecture(s).

➜ If you are using libraries that are not meant for that platform and the library uses straight malloc() instead of SDRAM.malloc() and free() instead SDRAM.free() then it might create memory issues.

Double check if your screen library has any of that. If so you’ll have to modify the library so that it uses the SDRAM API and of course ensure those calls are made after the call to begin() ➜ if they allocate memory in the constructor you’ll have to move it out into some init() or begin() function that you’ll call from setup() after calling SDRAM.begin() (and make sure nothing is done in the constructor that requires that buffer) if you leave stuff there.

---

<div class="post-metadata">

**Author:** ![lapowels](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/lapowels/32/1126500_2.png) [@lapowels](https://forum.arduino.cc/u/lapowels)\
**Post date:** [July 27, 2025, 8:52pm UTC](https://forum.arduino.cc/t/malloc-free-issues/1397078/12 "2025-07-27T20:52:59Z")

</div>

If I change the “display” library, do I move it into one of my Sketch’s tabs so it does not get automatically updated when the community updates it?

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [July 28, 2025, 12:56am UTC](https://forum.arduino.cc/t/malloc-free-issues/1397078/13 "2025-07-28T00:56:53Z")

</div>

Yes, you would copy the library files inside your project, modify the include to use the double quotes instead of the \<\> to tell to the compiler to look for the file locally before going looking in the other standard places.

Not sure this is your issue though…

---

<div class="post-metadata">

**Author:** ![embeddedkiddie](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/embeddedkiddie/32/1081520_2.png) [@embeddedkiddie](https://forum.arduino.cc/u/embeddedkiddie)\
**Post date:** [July 28, 2025, 1:18am UTC](https://forum.arduino.cc/t/malloc-free-issues/1397078/14 "2025-07-28T01:18:08Z")

</div>

> [@lapowels](#):
>
> is this an issue? how would I fix it?

According to the [compatibility list](https://docs.arduino.cc/libraries/arduinographics/#Compatibility) and the [release notes for the latest version v1.1.4](https://github.com/arduino-libraries/ArduinoGraphics/releases/tag/1.1.4), it seems that [ArduinoGraphics](https://github.com/arduino-libraries/ArduinoGraphics) does not work with GIGA, so I think the only way is to use LVGL when you display something on the GIGA Display Shield.

I recommend that you first [configure LVGL](https://docs.lvgl.io/master/details/integration/framework/arduino.html#configure-lvgl), then run the [examples in "GIGA Display Shield LVGL Guide"](https://docs.arduino.cc/tutorials/giga-display-shield/lvgl-guide/) or [LVGLDemo example in Arduino\_H7\_Video](https://github.com/arduino/ArduinoCore-mbed/tree/main/libraries/Arduino_H7_Video/examples/LVGLDemo), in order to become familiar with LVGL.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [January 24, 2026, 1:18am UTC](https://forum.arduino.cc/t/malloc-free-issues/1397078/15 "2026-01-24T01:18:22Z")

</div>

Closed on January 24, 2026, 1:18am UTC

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