Tool/software: Code Composer Studio
With an old GCC compiler version, I used the following code:
check_data();
{...
unsigned char data_count; //data size
data_count=eeprom_readChar(EADR_DATA_COUNT); //Read the data size from the EEPROM
unsigned char data_string[data_count]; //Dynamically memory allocation of a data string --> Produces error with CCS
…
}
With that old GCC compiler version, this code works fine, and allocate the memory to the data_string variable.
Now, I want use the same code with code composer studio. But every time it runs, this code produces a reset, when it arrives at the definition line of the dynamically allocated array "unsigned char data_string[data_count];".
Now I tried to allocate it with malloc() and free().
check_data();
{...
unsigned char data_count; //data size
data_count=eeprom_readChar(EADR_DATA_COUNT); //Read the data size from the EEPROM
char *data_string=malloc(data_count); //Dynamically memory allocation of a data string --> Works OK with CCS
...
free(data_string);
}
This code works fine, but I read that the use of malloc() and free() isn't very recommended for small microcontrollers.
One other solution is to allocate a fixed length (to the max length). In our case, the data_string have a length from 2 to 100 Bytes (Max). But in most cases it is under 10, so I prefer a dynamic allocation to a fixed size allocation.
Do you know an alternative declaration of a runtime dynamically allocated array?