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)?

조회 수: 14 (최근 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 CenterFile Exchange에서 MATLAB Compiler에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

제품


릴리스

R2010a

Community Treasure Hunt

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

Start Hunting!

Translated by