How to know line with segmentation fault in my code?

I'm trying to learn C pointers these days, when I am trying to deal with string and pointers, and link list I am frequently getting segmentation errors. And I wasn't able to fix these issues (I can't find out the exact line where the segmentation fault is happening). My questions are:

  • What is the tool to find the line with the segmentation fault in my code?

  • Is there any material (book or tutorial) that you can suggest to me to know about all issues related to the segmentation faults, their reasons and solutions?

  • gdb output:

    /home/sri/Documents/projects/practice/c/strings/a.out...done.
    (gdb) run
    Starting program: /home/sri/Documents/projects/practice/c/strings/a.out 
    Missing separate debuginfo for /lib64/ld-linux-x86-64.so.2
    Try: zypper install -C "debuginfo(build-id)=ecb8ef1a6904a2a3ec60a527f415f520c8636158"
    Missing separate debuginfo for /lib64/libc.so.6
    Try: zypper install -C "debuginfo(build-id)=bd1473e8e6a4c10a14731b5be4b35b4e87db2af7"
    this is print1 char *p 
    
    Program received signal SIGSEGV, Segmentation fault.
    0x00000000004005dd in do_print2 (p=0x7fffffffdda0) at string_orguments.c:16
    16      strcat(p[0],"added");
    (gdb) 
    

    As OS detects memory right violation by a process -- An invalid access to valid memory gives: SIGSEGV .

    In output:

    Program received signal SIGSEGV, Segmentation fault.
     0x00000000004005dd in do_print2 (p=0x7fffffffdda0) at string_orguments.c:16
     16     strcat(p[0],"added");
    

    Shows at strcat(p[0],"added"); you are trying to write on a memory that is for read only - invalid operation on valid memory.

    Note: strcat function at very first step try to write char a from second argument string literal "added" at a position where is in p[0] . So you might getting the signal SIGSEGV because write operation is invalid in place of (possible writing on read only memory).

    Also note: access to an invalid address gives: SIGBUS

    Read: strcat() implementation works but causes a core dump at the end To understand better.

    Finding segmentation fault algorithmically is Unsolvable problem, you don't have any tool (automation tool) for this purpose. But Only way it to bug your code using gdb like tools. Additionally to avoid segmentation fault read language principals/ruls precisely. You should be clear why segmentation fault occurs. then only by practice and experience you can learn How to write less buggy and more debuggable code.

    Also you should understand segmentation fault happens at runtime. may be your code run fine for some set of values but not for some set of values. So segmentation can't be detected at compilation time like.

    This also says why its unsolvable problem, because we can't write general algorithm that generates randomly infinite inputs and to test whether your code works perfectly!

    链接地址: http://www.djcxy.com/p/43896.html

    上一篇: 在python绑定中可能出现分段错误的原因是什么?

    下一篇: 如何知道我的代码中的分段错误?