Create global variable inside setup

I know the proper way is to declare outside, but here is the thing.
I have a microSD reader, and I want to store the data in an array. So before setup, it doesn't know how big has to be.

Example code:

unsigned char values[];

it's a project that takes lots of memory, so I can't declare an oversized value

maybe some alloc or malloc to resize the space for the array (I don't know how those work)
or maybe some global class with a variable that can be resized

what you think?

Thanks :kissing_heart:

I think you need to tell us more

no if you are going to create dynamic array you can create it inside a function and return pointer. but if you haven’t done dynamic memory management before, create oversized static array and live a happy life

malloc() is okay. Declare a global pointer to the right data type and malloc() it when the required size is known.

okay here it is a representation a bit more extended

#define NUM_ROW 4
#define NUM_COL 4

unsigned char val[NUM_ROW*NUM_COL];

void setup() {
  if (SD.begin(A0)) {
    error("initializing SD failed! ");

void readConfig(){
  File myFile ="config.txt");
  if (!myFile) {
    error("opening conf");

  unsigned int count=0;
  while (myFile.available()) {
    unsigned char c =;


Again, this is a representation. The first line that i read from the SD give me the dimensions

In your plan, what would happen if it was discover that more memory than is available is required?

What else is competing for memory? If the rest of the code makes known demands, just give the rest to the array in a static declaration.


The dimensions for what? What Arduino are you using?

I infer from your code that the dimensions you are talking about are row and column. Without knowing your actual application if you MUST read the entire file into memory then I suggest you read the dimensions, calculate the memory required, check that the memory does not exceed some threshold, and only then dynamically allocate the memory.

Keep in mind this is RAM we are talking about. Also, you will have to ensure you leave enough memory to support stack operations and any other dynamic allocation you will be performing.

i'm making a keyboard, and the microSD stores the values of each key, and im planing to store multiple layers. all of that with arduino pro micro, i'm just begining with the project and it says 59% of the space is used for the sketch, maybe its for the libraries SPI.h SD.h and Keyboard.h that uses almost all the space.

ensuring the space for all data of the SD i will do it later, at the moment I don't even know how to organize the data on the SD

you should start worrying about memory when you run out of it, 59% is nothing

That would be 59% of flash. When you read the SD card into memory that goes into RAM.

It'd look something like this:

unsigned char *values = NULL;
void setup(){
  int size = * 128;  // compute size (stored in blocks, or whatever.
  values = malloc(size);
  if (values != NULL) {, size);

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