Find the answer to your Linux question:
Results 1 to 7 of 7
Hi, Previously I was writing C/C++ codes in windows. Recently when I started to write codes under Fedora 9, I am getting a problem. The problem is that most of ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jan 2008
    Posts
    28

    Fedora 9: Receiving "Segmentation fault" in running C/C++ codes,


    Hi,

    Previously I was writing C/C++ codes in windows. Recently when I started to write codes under Fedora 9, I am getting a problem. The problem is that most of the times when I run my program, after compiling with GNU g++, it gives "Segmentation Fault" error. I looked around in books searching for the roots of this error. In a book, the writer told that this error error arises if we open too many files at the same time. But in my code I am not even opening or writing even to a single file.
    I tried my best but still I am not able to debug this error. Can any humane expert out there help me out in this regard. I would really appreciate his/her effort.

    Regards,
    Ehsan ul haq

  2. #2
    Linux Engineer hazel's Avatar
    Join Date
    May 2004
    Location
    Harrow, UK
    Posts
    1,257
    Do you get these seg faults with C programs too or only with C++? If the latter, I can't help you much, but the ones I get in C are invariably due to misuse of pointers.

    The way I debug them is to put one or more printed messages into my program and check to see if the message prints before the program crashes. By moving these messages about and recompiling I can usually zero in on the line that's causing the problem. Crude but quite effective.
    "I'm just a little old lady; don't try to dazzle me with jargon!"
    www.hrussman.entadsl.com

  3. #3
    Just Joined!
    Join Date
    Jan 2008
    Posts
    28
    Thanks for replying. Well, initially I also thought and tried the same procedure. But to my surprise it did not give error on any pointer operation. It simply gives "Segmentation fault", whenever i run the program.

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Engineer hazel's Avatar
    Join Date
    May 2004
    Location
    Harrow, UK
    Posts
    1,257
    Quote Originally Posted by ehsanulhaq View Post
    Thanks for replying. Well, initially I also thought and tried the same procedure. But to my surprise it did not give error on any pointer operation. It simply gives "Segmentation fault", whenever i run the program.
    "Segmentation fault" means that your program has tried to access memory that was not assigned to it. The Linux kernel polices memory rigorously so that programs cannot run wild and overwrite each other (which happens fairly often in Windows, causing all manner of freezes and system crashes).

    As I said, the usual cause of attempted illegal access to memory is that you have done something wrong with a pointer but you won't actually be told this; you just get this segfault message. That's why it is essential to find out at precisely what point in your program the crash occurs. Then you have a chance of working out what is wrong with the code at that point.

    Are you sure you have initialised any pointers correctly? If you have used a library like gtk that requires an initialisation function to be run first, have you included it? Have you checked that open files return a real file pointer and not a NULL? Those are the kind of mistakes that I have made in the past.

    Actually in my experience a new program nearly always crashes with a segfault the first time it's run. That's simply a consequence of the fact that C is such a powerful language and allows you to do so many silly things - plus the fact that Linux would rather crash your program than crash itself.
    "I'm just a little old lady; don't try to dazzle me with jargon!"
    www.hrussman.entadsl.com

  6. #5
    Just Joined!
    Join Date
    Jan 2008
    Posts
    28

    Smile

    Thanks again.

    I would like to discuss another very strange behaviour of my code. After receiving "segmentation fault" when I back traced my code I found out that it was getting stuck at writing to a file named "err_loc_file". I wrote some debug messages where i was opening the file to check if it's returning a null pointer. But to my surprise it was not. With nothing else to do I changed the name of the file to "e_log". Wow! the code ran perfectly ?

    Now this "Segmentation fault" really stuck me. When I change the name back the program fails. When I change the name randomly then sometimes it run and sometimes it doesn't. Can I have your comments on this please?

  7. #6
    Linux Engineer hazel's Avatar
    Join Date
    May 2004
    Location
    Harrow, UK
    Posts
    1,257
    Quote Originally Posted by ehsanulhaq View Post
    Thanks again.

    I would like to discuss another very strange behaviour of my code. After receiving "segmentation fault" when I back traced my code I found out that it was getting stuck at writing to a file named "err_loc_file". I wrote some debug messages where i was opening the file to check if it's returning a null pointer. But to my surprise it was not. With nothing else to do I changed the name of the file to "e_log". Wow! the code ran perfectly ?

    Now this "Segmentation fault" really stuck me. When I change the name back the program fails. When I change the name randomly then sometimes it run and sometimes it doesn't. Can I have your comments on this please?
    I agree, it's weird. The only thing I can suggest is that it has to do with the length of the filename. Are you storing this name in an array or buffer anywhere? Buffer overflows are a very common cause of segmentation faults and I notice that the name that works is a lot shorter than the one that doesn't.
    "I'm just a little old lady; don't try to dazzle me with jargon!"
    www.hrussman.entadsl.com

  8. #7
    Just Joined!
    Join Date
    Jan 2008
    Posts
    28
    Thanks hazel for your kind reply and interest.

    I am leaving this problem now and will take a deeper look at it on weekend. I will let you know as soon as I am getting any progress.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •