Bug 19254 - [3.3 Regression] Dynamically sized static multidimensional array access in constructor uses wrong address
Summary: [3.3 Regression] Dynamically sized static multidimensional array access in co...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 3.3.5
: P2 normal
Target Milestone: 3.4.0
Assignee: Not yet assigned to anyone
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2005-01-04 15:03 UTC by Sebastian Seifert
Modified: 2005-04-30 13:18 UTC (History)
1 user (show)

See Also:
Host: i486-linux ?
Target: i486-linux ?
Build: i486-linux ?
Known to work: 3.4.0 4.0.0 2.95.3
Known to fail: 3.3.3 3.2.3
Last reconfirmed: 2005-04-07 07:52:54


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Sebastian Seifert 2005-01-04 15:03:18 UTC
When inside a constructor a multidimensional array is declared whose size
depends on a variable, e.g.  int n = 5; int a[n][n];, address calculation for
accessing the array elements is wrong. Example code:


--- snip ---
int result;

class A {
        public:
        A();
};

void remember(int i) {
        result = i;
}

void test() {
        int n = 5;
        int a[n][n];

        a[0][4] = 42;
        a[4][0] = 43;

        remember(a[0][4]);  // a[0][4] still is 42
}

A::A() {
        int n = 5;
        int a[n][n];

        a[0][4] = 42;
        a[4][0] = 43;  // overwrites a[0][4]!

        remember(a[0][4]);  // a[0][4] is 43
}

int main(int argc, char** argv) {
        A* object = new A();
        return result;
}

--- snip ---

In the above code, the constructor will set result to 43. The identical code of
test() will set result to 42.

The assembler code of test() and the constructor differs slightly: in test(), n
is stored in %ecx, moved to %eax for some computation, and is later moved to
%eax a second time. In the constructor, however, n is moved to %eax directly and
later, where test() would movl %ecx, %eax, the constructor does movl 0, %eax
instead.

$gcc --version
gcc (GCC) 3.3.5 (Debian 1:3.3.5-5)
Comment 1 Andrew Pinski 2005-01-04 18:41:23 UTC
Confirmed, only a 3.3 regression, it works with 3.4.0.
Comment 2 Gabriel Dos Reis 2005-04-30 13:18:00 UTC
Not critical for 3.3.6