Bug 26418 - App with assignment to *(ptr + member_returning_zero()) segfaults
Summary: App with assignment to *(ptr + member_returning_zero()) segfaults
Status: RESOLVED DUPLICATE of bug 11751
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.0.1
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
Depends on:
Reported: 2006-02-22 13:37 UTC by fuchsia.groan
Modified: 2006-02-22 14:04 UTC (History)
45 users (show)

See Also:
Known to work:
Known to fail:
Last reconfirmed:


Note You need to log in before you can comment on or make changes to this bug.
Description fuchsia.groan 2006-02-22 13:37:00 UTC
  This code segfaults when executed on gcc 4.01 on i586/linux (mandriva 2006)

  Works fine on gcc 3.x - couldn't find a newer version to test it on.

  PS I couldn't find anything in your bug writing guidelines what to put in "host triplet" etc...

gcc -v:

Using built-in specs.
Target: i586-mandriva-linux-gnu
Configured with: ../configure --prefix=/usr --libexecdir=/usr/lib --with-slibdir=/lib --mandir=/usr$
Thread model: posix
gcc version 4.0.1 (4.0.1-5mdk for Mandriva Linux release 2006.0) 

typedef unsigned int uint;

char* dummy;

struct Array {
    char**         ptr;

    void push(char* i)       { *(ptr+alloc()) = i; }

    uint alloc() {
       ptr = &dummy;
       return 0;


int main()
   Array test;
   char* arg = "fred";
   // can you please check check *test.ptr == arg, as that was what led
   // me to finding the bug wasn't
Comment 1 fuchsia.groan 2006-02-22 13:41:20 UTC
No compile options just "g++ test.cpp" generates faulty code
Comment 2 Andrew Pinski 2006-02-22 14:04:55 UTC

The C and the C++ standard does not say which of ptr and alloc() is evaluated first so GCC is producing code which evaluates ptr before calling alloc which is ok for C and C++.

*** This bug has been marked as a duplicate of 11751 ***