This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CCS/MSP430FR5994: CCS/MSP430FR5994: Dynamically memory allocation of an array with a variable length

Part Number: MSP430FR5994


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?

  • Dynamic array allocation uses malloc(), too. 8^)

    What error did you get and what version of CCS and the compiler are you using?

    What is your heap space?

  • The name of this language feature ...

    user1082833 said:
    unsigned char data_string[data_count]; //Dynamically memory allocation of a data string --> Produces error with CCS

    ... is variable length arrays, or VLA for short.  The implementation of VLA in the TI MSP430 compiler uses malloc.  Any use of VLA requires a large amount of malloc memory.  In your case, it is likely this call to malloc fails, finally resulting in the reset you see.  

    user1082833 said:
    Do you know an alternative declaration of a runtime dynamically allocated array?

    Directly calling malloc and free, like you show in your first post, is one solution to consider.

    You should also consider this ...

    user1082833 said:
    One other solution is to allocate a fixed length (to the max length).

    Keep in mind this is a local array.  It takes up space on the stack only while the function is executing.  If the overall amount of stack required, including this large local array, is practical for your system, then this is a reasonable solution.

    Please let me know if either of these solutions resolves the problem.

    Thanks and regards,

    -George

  • Thanks a lot for your responses.

    I use Code Composer Studio Version: 10.0.0.00010.
    The heap space is 160 Bytes. (Default value)

    For information:

    • When I use this declaration (unsigned char data_string[data_count]; ) in a function, I received no warning message from the compiler, but a reset.
    • When I place the same declaration in the "main()" function (just for testing purpose), the compiler returns " #179-D variable "data_string" was declared but never referenced", and makes a reset.
    • The declaration of "unsigned char data_string[data_count];" fails, even if the string length is only 2 Bytes. So it isn't a problem of large memory amount, but a declaration problem.
    • When I try to use the "char *data_string=malloc(data_count); " declaration instead, it works fine, without any warning message.



    So my question is first: Is there an error in the first "unsigned char data_string[data_count];" declaration?

    Secondly, what is according to you the inconvenient of using "malloc()" directly? Is it the fact that "Any use of VLA requires a large amount of malloc memory."?

    If I understand it right, it could be better, memory speaking, to initialise a fixed size array than a VLA. Is it right?

  • user1082833 said:
    Is there an error in the first "unsigned char data_string[data_count];" declaration?

    The syntax is correct.  The underlying language feature, VLA, is supported.  However, because VLA requires more malloc memory than you have available, the code fails when it runs.

    user1082833 said:
    what is according to you the inconvenient of using "malloc()" directly?

    I did not say calling malloc directly is inconvenient.  I said it is a solution to consider.

    user1082833 said:
    If I understand it right, it could be better, memory speaking, to initialise a fixed size array than a VLA.

    That's correct, it could be better.  If you choose to implement this method, make sure you are able to allocate enough stack for the system, including this large local array.

    Please let me know if either of these two suggested solutions resolves the problem.

    Thanks and regards,

    -George

  • Thank you for your reply, that helps me to better understanding the memory use in VLA.

    I think, that I will use the second solution, and initialise a fixed size array. This seems more secure to me.