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 ビュー (過去 30 日間)
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

採用された回答

MathWorks Support Team
MathWorks Support Team 2010 年 7 月 20 日
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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMATLAB Compiler についてさらに検索

タグ

タグが未入力です。

製品


リリース

R2010a

Community Treasure Hunt

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

Start Hunting!

Translated by