How to use dynamic arrays with PolySpace Client for C/C++?

1 view (last 30 days)
I see the following error message during PolySpace compilation phase when I use dynamic arrays in my code:
Verifying sample.c
"c:\tmp\mds2\sources\sample.c", line 4: error: expression must have a constant value
char b[a];
^
1 error detected in the compilation of "sample.c".

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 13 Dec 2010
Dynamic arrays are a feature of the C99 standard (called also variable-length arrays) and they cannot be actually used with PolySpace Client for C/C++ (C language).
As a workaround, replace the declaration of the array by a declaration containing a call to malloc. For example:
void foo(unsigned char a)
{
char b[a];
}
can be replaced by :
#include <stdlib>
void foo(unsigned char a)
{
#ifndef POLYSPACE
char b[a];
#else
char *b = (char *) malloc(sizeof(char) * a);
#endif
}
The goal of the #ifndef directive is to keep the original code when the C file is used with a compiler, and to use the malloc when the C file is analyzed with PolySpace.
Note: the compilation flag POLYSPACE should be added to the list of flags (-D option).

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!