My source file contains some manipulation on struct arrays. After the
initialization of some local data, there is a call to a dummy function which
has its actual argument being the value of a field of one element of such an
array.
In running the executable compiled with -O, memory corruption can be observed.
Note that if the call to the dummy function is removed, the generated code runs
correctly.
Here is the example:
% cat tst.c
#include <stdio.h>
typedef struct {
int re, im;
} cint32_T;
static void m_foo(int i)
{}
int main()
{
static cint32_T sdata[3] = { { 504094, -1909779 }, { 658988, -552759 }, {
-15591, -896799 } };
cint32_T ldata[3];
cint32_T odata[3];
int i;
for(i = 0; i < 3; i++) {// assign ldata from sdata
ldata[i] = sdata[i];
}
for(i = 0; i < 3; i++) {// assign odata from ldata
odata[i] = ldata[i];
}
m_foo(ldata[2].re); // make a dummy call
// Now data stored in odata[0] is corrupted.
printf("%d, %d\n", odata[0].re, odata[0].im);
return 0;
}
% gcc -Wall tst.c -o correct
% correct
504094, -1909779
% gcc -Wall tst.c -O -o wrong
% wrong
0, 0
%
--
Summary: Memory corruption observed in executable generated from
struct array with compiler option -O
Product: gcc
Version: 4.2.3
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: xqi at mathworks dot com
GCC build triplet: x86_64-unknown-linux-gnu
GCC host triplet: x86_64-unknown-linux-gnu
GCC target triplet: x86_64-unknown-linux-gnu
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=41382