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