Initializing Memory

I am coding in C and am experiencing a memory leak. I allocate memory via:

pDatabaseHead = (unsigned char*)SDRAM.malloc(DATA_BASE_SIZE_CHAR);

and am initializing it via:

memset((unsigned char*)pDatabaseHead, 0, DATA_BASE_SIZE_CHAR); // Initialize the database to zeros

This "memset" partially destroys my Giga Display. If I change the above limit to: "DATA_BASE_SIZE_CHAR/8" then only a portion of my display is blanked out. BUT if I explicitly zeroize memory via:

for (i=0; i<DATA_BASE_SIZE_CHAR; i=+1) *(pDatabaseHead+i) = 0;

Then the screen display is not wiped out. These two methods should do the same thing but the "memset" should do it faster and I would rather use it.

Any Ideas/suggestions?

Thanks

Is this correct?

Did you mean i+=1 ?

1 Like

Hi @lapowels , as @mancera1979 suggests, check that loop increment. As to the display corruption, I'd check to see if SDRAM on your board and the display use the same memory area as this would explain the corruption.

p.s. assume you’re doing an SDRAM.begin()

A bit of history: a long time ago i=+1 did mean increment i by 1.
(The C Programming Language, first edition)

1 Like

OR i++?

I updated my code to:

for (i=0; i<DATA_BASE_SIZE_CHAR; i++) *(pDatabaseHead+i) = 0;

and now this corrupts the GIGA Display. I think your on the right path in saying that my EEPROM memory is mapped into my GIGA Display Memory. How do I tell what is mapped where?

Thanks

Don't have a Giga, but looking at some example code

void frameBuffer() {
    // In case we want a framebuffer-like area at the beginning of the flash,
    // simply initialize the memory as

    SDRAM.begin(SDRAM_START_ADDRESS + 2 * 1024 * 1024);
    // 2MB of contiguous memory available at the beginning

    uint32_t* framebuffer = (uint32_t*)SDRAM_START_ADDRESS;

    // We can't allocate anymore the huge 7MB array

    uint8_t* myVeryBigArray = (uint8_t*)SDRAM.malloc(7 * 1024 * 1024);
    if (myVeryBigArray == NULL) {
        Serial.println("Oops, too big :)");
    }

}

Did the call to SDRAM.begin set aside space for a framebuffer, that would not be used as "plain RAM" with malloc?

How big is DATA_BASE_SIZE_CHAR?

Just checking: did the malloc actually succeed, or did it return nullptr?

Minor points:

  • If pDatabaseHead is already an unsigned char *, why cast it again to pass it to memset?
  • There's no need to cast, since memset takes void *: any pointer will do
  • pDatabaseHead[i] reads better and is easier to type than *(pDatabaseHead+i)

Just to let the community know that this memory leak was solved by moving the SDRAM initialization call before the Display initialization call

SDRAM.begin();
Display.begin();

I had built the display and pushed it onto the display(my Splash Screen) and then initialized the SDRAM later on. I spent many hours taking my program apart to resolve this issue.

Thanks for all your input.

1 Like