4 years, 5 months ago.

How to make a class member point to an array located in flash?

Hello, I'm creating a class for storing bitmaps which contains attributes such as width, height, as well as a pointer to an array for the pixel data. If a program using the class wants to, say, create an object and read it in from a file system, it will need to obviously use RAM, however, I'd also like the option to pre-define some read only bitmaps from arrays stored in flash. From what I have been reading using the const declaration will cause a pre-initialized array to be stored in flash, but the compiler won't allow me to initialize my class member pointer to point at that array. If I try to use const for the entire object instance, it still uses up RAM space as well. In the end I want pre-defined bitmaps to reside in flash, while newly created/loaded ones reside in RAM. Is this even possible?

1 Answer

4 years, 5 months ago.

A const declared array will indeed reside in flash. You will need a typecast to make that work with a regular pointer:

const char array[] = {0,1,2,3,4,5};
char *myarray;

main {
  myarray = (char *) array;

Accepted Answer

Perfect. Thanks! I was just doing the wrong typecast. This will save me a ton of RAM! :D

posted by Douglas S 12 Dec 2016