Why do I get a false "array subscript is above array bounds" warning message when compiling my MEX file with GCC on MATLAB 7.10 (R2010a)?

13 views (last 30 days)
When I compile the following code:
typedef struct {
int chunks[3];
} S;
void f(int y[])
{
int nc;
int i = 0;
for (nc = 0; nc < 3; nc = nc + 1) {
i = i + 1;
}
if (i < 3) {
y[i] = 0;
}
}
int main(void)
{
S tmp;
f(tmp.chunks);
return 0;
}
with this command:
gcc -Wall -O3 s3.c
I get the following warning message:
s3.c: In function 'main':
s3.c:15: warning: array subscript is above array bounds

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 20 Jul 2010
This is a bug in GCC 4.3.4. For more information on this, please see the following webpage:
<http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43949>
GCC 4.4.3 has this bug fixed.
This is a false warning, and can be ignored. The -O3 switch inlines the function f() within the function main(). Now, when analyzing the inlined code, the compiler knows that y has 3 elements. However, it failes to consider the fact that on line 15, i is guaranteed to be less than 3 by the condition on line 14, so out of bounds subscript is not possible.
If the -Werror switch is specified, the warning is treated as an error, and the compilation will fail.
To work around this issue, you can either:
1. Compile the code with the –Wno-array-bounds switch
2. If in a header file, add the following code fragment to the header
// suppress bogus warning when compiling with gcc 4.3
#if (__GNUC__ == 4 && __GNUC_MINOR__ == 3)
#pragma GCC diagnostic ignored "-Warray-bounds"
#endif

More Answers (0)

Categories

Find more on MATLAB Compiler in Help Center and File Exchange

Tags

No tags entered yet.

Products


Release

R2010a

Community Treasure Hunt

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

Start Hunting!