23LC512 to save references extern


I am working on a project where I have a class (Container) to store the addresses of objects from another class (class Object), therefore i implemented a sort of container class. Unfortunately the SRAM of my Arduino Mega is too small because I have quite a lot of them. My plan now is to use a 23LC512 and the SPI library to store them extern. Therefore I tried to split up the 2 byte address of the object and store the two bytes at an address in the 23LC512 which i choose when I initialize my container. When i want to use the object I call the 2 bytes and put them back together. My idea looks something like this:

class Container
   uint32_t address;
   byte count;
   Container(uint32_t address32)
     count = 0               //counter to know how many addresses have been added 
     address = address32;    //address where I want to store it in the 23LC512

   void addObject(Object* objectp)
      //split up the address
      uint8_t buffer1 = (uint16_t)((objectp) & 0xFF;
      uint8_t buffer2 = (uint_16_t)((objectp >> 8) & 0xFF;

      //write the split up address into the 23LC512
      24LC512Write8(address + count, buffer1);
      24LC512Write8(address + count + 1, buffer2);

      //add 1 to counter

   uint16_t getAddress(int number)
     //read the address of a specific Object in the Container
     uint8_t buffer1 = 23LC512Read8(address + number);
     uint8_t buffer2 = 23LC512Read8(address + number + 1);
     //put it back together and return it
     return (buffer1 | (buffer2 << 8));

   void foo(int i)
      //use the address as pointer to call a function from the Object - class
      Object* objectp = getAddress(i);

Container container1(0x00FF);

This does of course not work because in

uint8_t buffer1 = (uint16_t)((objectp) & 0xFF;

“objectp” is a pointer to object and not a “uint16_t”.
And vise versa in

Object* objectp = getAddress(i);

I cant put a “uint16_t” into a “objectp”.
Is there any way I can convert them or does anyone have a suggestion how I can fix my problem on another way?
Thank you for your answers!

Looks like an interesting project. Have you considered a ESP32, I believe it the ESP32 chip contains 520KB of ram. It is a 32 bit processor with two cores (some versions) and you can use the Arduino IDE to work with it.