Possible reasons.
1. pointer uninitialized
So remember to initialize after defining the pointer and decide whether it is NULL or not when using it.[easie@localhost zxxtest]$ g++ -o bugging -g bugging.cc #The compiler gets the executable file, so you have to add - g to debug it. [easie@localhost zxxtest]$ ./bugging #Running executable files please input a string:zxx Segmentation fault (core dumped) #Segmentation fault [easie@localhost zxxtest]$ gdb bugging #Debugging with GDB GNU gdb (GDB) Fedora (7.4.50.20120120-54.fc17) Copyright (C) 2012 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty" for details. This GDB was configured as "i686-redhat-linux-gnu". For bug reporting instructions, please see: <http://www.gnu.org/software/gdb/bugs/>... Reading symbols from /home/easie/test/Aqua-Sim-1.0/zxxtest/bugging...done. (gdb) l #Source program, the error should be that string is not initialized before the gets function receives the string #Buffe can be used as string initialization, in order to avoid security risks, use scanf to receive 1 #include "cstdlib" 2 #include "cstdio" 3 static char buff[256]; 4 static char *string; 5 int main() 6 { 7 for(int i=0;i<256;i++){buff[i]=0;} 8 //string=buff; 9 printf("please input a string:"); 10 gets(string); (gdb) r #Operation procedure Starting program: /home/easie/test/Aqua-Sim-1.0/zxxtest/bugging please input a string:zxxProgram received signal SIGSEGV, Segmentation fault. #Segmentation fault _IO_gets (buf=0x0) at iogets.c:5555 buf[0] = (char) ch; (gdb) where #where is very useful, find the error directly in line 10 #0 _IO_gets (buf=0x0) at iogets.c:55 #1 0x0804851c in main () at bugging.cc:10 (gdb)list #Maybe the pointer hasn't been initialized, and list ing after every run is not the original source program. I haven't found the reason yet. 50 /* This is very tricky since a file descriptor may be in the 51 non-blocking mode. The error flag doesn't mean much in this 52 case. We return an error only when there is a new error. */53 int old_error = _IO_stdin->_IO_file_flags & _IO_ERR_SEEN; 54 _IO_stdin->_IO_file_flags &= ~_IO_ERR_SEEN; 55 buf[0] = (char) ch; 56 count = INTUSE(_IO_getline) (_IO_stdin, buf + 1, INT_MAX, '\n', 0) + 1; 57 if (_IO_stdin->_IO_file_flags & _IO_ERR_SEEN) 58 { 59 retval = NULL; (gdb) list 7 2 This file is part of the GNU C Library. 3 4 The GNU C Library is free software; you can redistribute it and/or 5 modify it under the terms of the GNU Lesser General Public 6 License as published by the Free Software Foundation; either 7 version 2.1 of the License, or (at your option) any later version. 8 9 The GNU C Library is distributed in the hope that it will be useful, 10 but WITHOUT ANY WARRANTY; without even the implied warranty of11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU(gdb) main 7 signal Undefined maintenance command: "7 signal". Try "help maintenance". (gdb) man 7 signal Undefined command: "man". Try "help". (gdb) man 7 signal|grep SEGV Undefined command: "man". Try "help". (gdb) Q A debugging session is active.Inferior 1 [process 17662] will be killed. Quit anyway? (y or n) Y [easie@localhost zxxtest]$ man 7 signal|grep SEGV #See SIGSEGV information signals through man 7 signal, such as SIGSEGV and SIGFPE. generated as a consequence of exe‐ SIGSEGV 11 Core Invalid memory reference
The revised procedure is as follows:
1 #include "cstdlib" 2 #include "cstdio" 3 static char buff[256]; 4 static char *string; 5 int main() 6 { 7 for(int i=0;i<256;i++){buff[i]=0;} 8 string=buff; //Initialize string 9 printf("please input a string:"); 10 // gets(string); 11 scanf("%[^\n]",string);//Get the string of screen input
2. Whether the array is initialized, whether the subscript of the array crosses the boundary, whether the element of the array exists, etc.
For example:
#include <stdio.h> int main() { char test[1]; printf("%c", test[1000000000]); return 0; }
3. Whether the format control of variables is reasonable when dealing with variables, etc.
For example:
For example, try to output or store char or int according to% s. Similarly, there are format control problems such as sprintf.#include <stdio.h> int main() { int b = 10; printf("%s/n", b); return 0; }
II. Debugging Method#include <stdio.h> #include <string.h> char c='c'; int i=10; char buf[100]; printf("%s", c); //Trying to output char in string format printf("%s", i); //Trying to output int as a string memset(buf, 0, 100); sprintf(buf, "%s", c); //Trying to convert char type to string format memset(buf, 0, 100); sprintf(buf, "%s", i); //Trying to convert int type to string
Use GDB debugging, refer to the first article, use r, where, list and other commands
Reference article: http://blog.csdn.net/love_gaohz/article/details/6597857