Thread: strange gcc problem...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Location
    China
    Posts
    74

    Unhappy strange gcc problem...

    Code:
    #include <stdio.h>
    
    static void DoSwapPointer(void** pointer1, void** pointer2)
    {
        void* tempPointer1 = *pointer1;
        void* tempPointer2 = *pointer2;
        *pointer1 = tempPointer2;
        *pointer2 = tempPointer1;
        return;
    }
    
    int main()
    {
        int data1 = 15, data2 = 30;
        int* ptr1 = &data1;
        int* ptr2 = &data2;
    
        printf("DBGA %d %d\n", *ptr1, *ptr2);
        DoSwapPointer((void**)&ptr1, (void**)&ptr2);
        printf("DBGB %d %d\n", *ptr2, *ptr2);
        return 0;
    }
    I compile these code with "gcc main.c". The result is
    Code:
    DBGA 15 30
    DBGB 30 15
    But if I compile them with "gcc -O3 main.c". I will get
    Code:
    DBGA 15 30
    DBGB 15 30
    Isn't it strange? Why would this happen?

  2. #2
    Registered User
    Join Date
    Mar 2010
    Location
    China
    Posts
    74
    I posted the wrong code. The second "printf" line is "printf("DBGB %d %d\n", *ptr1, *ptr2);".

  3. #3
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    You need a "volatile " in your declarations of your pointers.

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    I can't reproduce the bug with gcc version 4.2.1. The O3 optimization level can sometimes break code, and I guess your version of GCC has an optimization bug in it.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    Quote Originally Posted by User Name: View Post
    You need a "volatile " in your declarations of your pointers.
    No, that's not how you use volatile. The volatile keyword is needed when the compiler can't see the flow of the program, i.e. when interrupts can happen or when dealing with memory mapped I/O.

  6. #6
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Read up on pointer aliasing.

    No, this is not a bug in GCC. It's a bug in your code.

  7. #7
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    It's not a bug in GCC, it's ambiguity in your code. Tell GCC want you want, and it'll give it to you, be vague and tell GCC to cut out everything not needed, GCC will probably assume something that is needed is not because you're vague.

  8. #8
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    volatile will fix it, because it essentially disables all aliasing assumptions.

    But that's not how volatile is intended to be used.

  9. #9
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    I'll admit I'm no C guru. What is more correct?

  10. #10
    Registered User
    Join Date
    Mar 2010
    Location
    China
    Posts
    74
    Using "volatile" is right. I added "volatile" in my declaration and it works correctly under -O3 optimization.

    Code:
    static void DoSwapPointer(void** volatile pointer1, void** volatile pointer2)
    {
        void* tempPointer1 = *pointer1;
        void* tempPointer2 = *pointer2;
        *pointer1 = tempPointer2;
        *pointer2 = tempPointer1;
        return;
    }

  11. #11
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    It's a lesser known rule of C/C++ -

    The compiler can assume that data will only be accessed by pointers of the same type.

    This is called strict aliasing.

    In this case you are accessing the int pointers using void double pointers, and the compiler assumed you won't do that, and decided that since DoSwapPointers takes in 2 void double pointers, it can't possibly change ptr1 and ptr2. Therefore it probably optimized out DoSwapPointers altogether.

    I'm not sure what's the "correct" way to fix that. Why are you using void double pointers instead of int double pointers in the first place?

  12. #12
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Or you can use -fno-strict-aliasing.

    But your code is still wrong.

  13. #13
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    I wonder why GCC doesn't produce the famous "dereferencing type-punned pointer will break strict-aliasing rules" warning?

  14. #14
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    It does if you use -Wall in combination with -O3.

    Code:
    cyberfish@cyberfish-desktop:/tmp$ gcc -Wall -O3 b.c
    b.c: In function ‘main’:
    b.c:7: warning: dereferencing pointer ‘ptr1.18’ does break strict-aliasing rules
    b.c:19: note: initialized from here
    b.c:8: warning: dereferencing pointer ‘ptr2.17’ does break strict-aliasing rules
    b.c:19: note: initialized from here
    And I get this output instead -
    Code:
    cyberfish@cyberfish-desktop:/tmp$ gcc b.c
    cyberfish@cyberfish-desktop:/tmp$ ./a.out
    DBGA 15 30
    DBGB 15 15
    cyberfish@cyberfish-desktop:/tmp$ gcc -O3 b.c
    cyberfish@cyberfish-desktop:/tmp$ ./a.out
    DBGA 15 30
    DBGB 30 30
    So it doesn't work even at -O0. The fact that it worked for you is just coincidence.

    And adding volatile doesn't change anything for me, surprisingly.

    This is GCC 4.4.1

  15. #15
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Uh oops I was running the uncorrected code.

    With the corrected code I get same results (no swapping with -O3).

    And volatile does "fix it".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange problem
    By G4B3 in forum C Programming
    Replies: 6
    Last Post: 05-14-2008, 02:07 PM
  2. Strange problem with classes in header files
    By samGwilliam in forum C++ Programming
    Replies: 2
    Last Post: 02-29-2008, 04:55 AM
  3. Strange gcc warning messages with derived classes
    By skewray in forum C++ Programming
    Replies: 5
    Last Post: 09-23-2007, 04:46 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. gcc compile problem
    By keyz in forum Linux Programming
    Replies: 3
    Last Post: 05-22-2003, 07:14 AM