Thread: segmentation fault ?

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    18

    Talking segmentation fault ?

    if we construct anonymous static union ,at file scope my compiler(gcc) gives error:segmentation fault
    why this is so!
    Code:
    static union{int a;};
    
    int main()
    {
        return 0;
    }
    it is simplified code,
    i'm using code::blocks 8.02 (it uses gcc).
    is it non standard thing or it's my compiler's fault?
    Last edited by hr1212s; 05-11-2010 at 10:19 AM.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    It's [/code] not [\code].
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    Your code doesn't make any sense. A static union definition that is never used? Although, it doesn't crash for me.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    I don't get a segmentation fault..

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    18
    Quote Originally Posted by Brafil View Post
    Your code doesn't make any sense. A static union definition that is never used? Although, it doesn't crash for me.
    it is simplified code,
    i'm using code::blocks 8.02 . is it non standard thing or it's my compiler's fault?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    So, you are saying that this simplified program demonstrates the problem?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    18
    Quote Originally Posted by laserlight View Post
    So, you are saying that this simplified program demonstrates the problem?
    yes it gives me error

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by hr1212s View Post
    it is simplified code,
    i'm using code::blocks 8.02 . is it non standard thing or it's my compiler's fault?
    It compiles and runs fine with gcc 4.3.2 (and g++).

    Are you talking about the "simplified" code example, or the "real code"?

    Quote Originally Posted by hr1212s View Post
    yes it gives me error
    What error?
    Last edited by MK27; 05-11-2010 at 10:25 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Oh, now I understand. I originally tried using the same compiler that is bundled with one Code Blocks installation package, but did not see any problem when I lazily compiled with no options.

    Now, I compiled with -Wall and -pedantic, and discovered that with -Wall the compiler has an internal error in which the error message states a segmentation fault with involved. The answer to your question is given right there: you are looking at a genuine compiler bug.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by laserlight View Post
    Oh, now I understand. I originally tried using the same compiler that is bundled with one Code Blocks installation package, but did not see any problem when I lazily compiled with no options.

    Now, I compiled with -Wall and -pedantic, and discovered that with -Wall the compiler has an internal error in which the error message states a segmentation fault with involved. The answer to your question is given right there: you are looking at a genuine compiler bug.
    Yes, I agree. In fact, any segfault from any program is a bug in the specific program. So even if your code is buggy as hell; if it makes the compiler crash, it's a problem on the compiler's side. It should in stead produce meaningful error messages on what you did wrong.

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    18
    oh thanks guys,
    during this i found link Anonymous Union
    which point to same conclusion that, this code is fine and error is bug of compiler

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    18

    Cool

    Quote Originally Posted by laserlight View Post
    Oh, now I understand. I originally tried using the same compiler that is bundled with one Code Blocks installation package, but did not see any problem when I lazily compiled with no options.

    Now, I compiled with -Wall and -pedantic, and discovered that with -Wall the compiler has an internal error in which the error message states a segmentation fault with involved. The answer to your question is given right there: you are looking at a genuine compiler bug.
    hey ,will you please give some idea about what -Wall and -pedantic does in code::blocks !
    i haven't find it on google.

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by hr1212s View Post
    hey ,will you please give some idea about what -Wall and -pedantic does in code::blocks !
    i haven't find it on google.
    GCC online documentation - GNU Project - Free Software Foundation (FSF)

  14. #14
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I guess you should upgrade your gcc, as I said there is no problem with 4.3.2, which is several years old (I think the current one is 4.4).

    -Wall is very useful, there must be some setting in Code::Blocks to turn this on by default.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  15. #15
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Actually I am still stuck trying to figure out what the point of a union with only one member could be...is this a result of the code simplification?
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Re: Segmentation fault
    By turkish_van in forum C Programming
    Replies: 8
    Last Post: 01-20-2007, 05:50 PM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM