当前位置: 技术问答>linux和unix
Linux下C编程问题:段错误,Program received signal SIGSEGV, Segmentation fault.请教各位!
来源: 互联网 发布时间:2015-05-09
本文导语: Linux下C编程问题: 段错误,Program received signal SIGSEGV, Segmentation fault.请教各位! 下面是我调试时的一些信息: [root@localhost Practice]# gcc -o p1 -g p1.c [root@localhost Practice]# ./p1 段错误 [root@localhost Practice]# gdb p1 GN...
Linux下C编程问题:
段错误,Program received signal SIGSEGV, Segmentation fault.请教各位!
下面是我调试时的一些信息:
[root@localhost Practice]# gcc -o p1 -g p1.c
[root@localhost Practice]# ./p1
段错误
[root@localhost Practice]# gdb p1
GNU gdb Red Hat Linux (5.3post-0.20021129.18rh)
Copyright 2003 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i386-redhat-linux-gnu"...
(gdb) run
Starting program: /usr/Practice/p1
Program received signal SIGSEGV, Segmentation fault.
0x420791b5 in strcat () from /lib/tls/libc.so.6
(gdb) where
#0 0x420791b5 in strcat () from /lib/tls/libc.so.6
#1 0x08048ac4 in __libc_csu_init ()
#2 0x0804872b in main (argc=1, argv=0xbfffdfe4) at p1.c:46
#3 0x42015574 in __libc_start_main () from /lib/tls/libc.so.6
(gdb) list
46 lineBuf = sample("/cpuinfo",1,0);
47 //strcpy(lineBuf,sample("/cpuinfo",1,0));
48 //printf("nCPU Information:n%s",sample("/cpuinfo",1,0));
49 //free(lineBuf);
50 lineBuf = sample("/cpuinfo",4,0);
51 printf("%s",lineBuf);
52 lineBuf = sample("/cpuinfo",6,0);
53 printf("%s",lineBuf);
54 //printf("nKill you!");
55
(gdb)
相关声明:
#define MAX_BUF_LINE 200
char * lineBuf = (char *)malloc(MAX_BUF_LINE);
char * sample(char *,const int,const int);
给lineBuf赋值时出错,
直接printf("nCPU Information:n%s",sample("/cpuinfo",1,0));也出错,
该了好久都没改好,请教大家,谢谢!
段错误,Program received signal SIGSEGV, Segmentation fault.请教各位!
下面是我调试时的一些信息:
[root@localhost Practice]# gcc -o p1 -g p1.c
[root@localhost Practice]# ./p1
段错误
[root@localhost Practice]# gdb p1
GNU gdb Red Hat Linux (5.3post-0.20021129.18rh)
Copyright 2003 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i386-redhat-linux-gnu"...
(gdb) run
Starting program: /usr/Practice/p1
Program received signal SIGSEGV, Segmentation fault.
0x420791b5 in strcat () from /lib/tls/libc.so.6
(gdb) where
#0 0x420791b5 in strcat () from /lib/tls/libc.so.6
#1 0x08048ac4 in __libc_csu_init ()
#2 0x0804872b in main (argc=1, argv=0xbfffdfe4) at p1.c:46
#3 0x42015574 in __libc_start_main () from /lib/tls/libc.so.6
(gdb) list
46 lineBuf = sample("/cpuinfo",1,0);
47 //strcpy(lineBuf,sample("/cpuinfo",1,0));
48 //printf("nCPU Information:n%s",sample("/cpuinfo",1,0));
49 //free(lineBuf);
50 lineBuf = sample("/cpuinfo",4,0);
51 printf("%s",lineBuf);
52 lineBuf = sample("/cpuinfo",6,0);
53 printf("%s",lineBuf);
54 //printf("nKill you!");
55
(gdb)
相关声明:
#define MAX_BUF_LINE 200
char * lineBuf = (char *)malloc(MAX_BUF_LINE);
char * sample(char *,const int,const int);
给lineBuf赋值时出错,
直接printf("nCPU Information:n%s",sample("/cpuinfo",1,0));也出错,
该了好久都没改好,请教大家,谢谢!
|
就错在pathName = strcat("/proc",fileName);这行
首先"/proc",是静态字符串,他的存储空间在编译期间已经分配,大小是固定的
你使用strcat,将fileName向"/proc"后面添加,当然会出错,
另外,你需要仔细阅读一下strcat的用法
首先"/proc",是静态字符串,他的存储空间在编译期间已经分配,大小是固定的
你使用strcat,将fileName向"/proc"后面添加,当然会出错,
另外,你需要仔细阅读一下strcat的用法
|
同意blh(老猫)的说法,你可以
char *tmp=(char *)malloc(100);
memset(tmp,0x0,100);
memcpy(tmp,"/porc",5);
然后将pathName = strcat("/proc",fileName);改为
pathName = strcat(tmp,fileName);
原因同blh(老猫)
char *tmp=(char *)malloc(100);
memset(tmp,0x0,100);
memcpy(tmp,"/porc",5);
然后将pathName = strcat("/proc",fileName);改为
pathName = strcat(tmp,fileName);
原因同blh(老猫)
|
char * lineBuf = (char *)malloc(MAX_BUF_LINE);
这句话有错,没有告诉运行时库你申请的内存数据是什么类型的,应改为:
char * lineBuf = (char *)malloc(sizeof (char) * MAX_BUF_LINE);
这句话有错,没有告诉运行时库你申请的内存数据是什么类型的,应改为:
char * lineBuf = (char *)malloc(sizeof (char) * MAX_BUF_LINE);
|
sorry,sizeof后面没有空格,敲错了
|
你的strcat在哪儿阿??????
|
把程序贴上来吧,这样看好累呀
|
http://expert.csdn.net/Expert/topic/2426/2426475.xml?temp=.4317896