LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1019|回复: 7

库函数怎么一下就没了?急!!

[复制链接]
发表于 2003-4-24 17:36:50 | 显示全部楼层 |阅读模式
在写增加条目的代码的过程中,写好后,刚开始调试时还好好的,突然在运行到 strcat()时卡住了,报错:Segmentation fault (core dumped) 。以前在这都很正常的运行下去,并用这个程序加了好几条条目。
    我用gdb调试,报错为:
     Program received signal SIGSEGV, Segmentation fault.
     0x400e0aad in strcat () from /lib/i686/libc.so.6
    我用ldd打开/lib/i686/libc.so.c
    显示:
    /lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000)
    又打开/lib/ld-linux.so.2
    显示:
    statically linked
    表示静态库已经连接。
    我在/usr/lib目录下找到C语言的标准库文件libc.so发现它变的和其他的库文件不同了,直接点就打开了,显示如下:
/* GNU ld script
   Use the shared library, but some functions are only in
   the static library, so try that secondarily.  */
   GROUP ( /lib/libc.so.6 /usr/lib/libc_nonshared.a )

   是不是库文件被破坏了?
   
在写增加条目的代码的过程中,写好后,刚开始调试时还好好的,突然在运行到 strcat()时卡住了,报错:Segmentation fault (core dumped) 。以前在这都很正常的运行下去,并用这个程序加了好几条条目。
    我用gdb调试,报错为:
     Program received signal SIGSEGV, Segmentation fault.
     0x400e0aad in strcat () from /lib/i686/libc.so.6
    我用ldd打开/lib/i686/libc.so.c
    显示:
    /lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000)
    又打开/lib/ld-linux.so.2
    显示:
    statically linked
    表示静态库已经连接。
    我在/usr/lib目录下找到C语言的标准库文件libc.so发现它变的和其他的库文件不同了,直接点就打开了,显示如下:
/* GNU ld script
   Use the shared library, but some functions are only in
   the static library, so try that secondarily.  */
   GROUP ( /lib/libc.so.6 /usr/lib/libc_nonshared.a )

   是不是库文件被破坏了?
发表于 2003-4-24 18:09:33 | 显示全部楼层
好像是段错误,可能你的程序引用了非法指针地址,如,你的指针变量没有进行赋值就使用。
发表于 2003-4-24 22:19:41 | 显示全部楼层
你检查一下叫 strcat() 时候给进去的参数对不对吧,看来是错了。
 楼主| 发表于 2003-4-25 08:41:06 | 显示全部楼层
谢谢先!

问题找到了,不是指针赋值的问题,但还是跟地址空间有关。是我将c语言的概念和数据结构中的东西给搞混淆了。
发表于 2005-1-25 00:25:49 | 显示全部楼层
i have "Sagmentation fault" error at strcat(), too

i don't know why , anyone can help me?

here is my code

[php]
#include <string.h>
#include <stdlib.h>

int main() {
  char* str="Hallo";
  char* ch="you";
  char* result=(char*)malloc(sizeof(char)*20);
  printf("%s\n", str);
  printf("%s\n", ch);
  result=strcat(str, ch);  //have error this line
  printf("%s\n", result);
  return 0;
}

[/php]
回复 支持 反对

使用道具 举报

发表于 2005-1-25 04:20:52 | 显示全部楼层
看来你就没有用过strcat,那么想当然的用当然要挂。
你还是看看manual page吧至少,否则这样的程序写出来就有点....
$man strcat

  1. STRCAT(3)                  Linux Programmer's Manual                 STRCAT(3)

  2. NAME
  3.        strcat, strncat - concatenate two strings

  4. SYNOPSIS
  5.        #include <string.h>

  6.        char *strcat(char *dest, const char *src);

  7.        char *strncat(char *dest, const char *src, size_t n);

  8. DESCRIPTION
  9.        The  strcat()  function appends the src string to the dest string over-
  10.        writing the `\0' character at the end of dest, and then adds  a  termi-
  11.        nating  `\0'  character.   The  strings  may  not overlap, and the dest
  12.        string must have enough space for the result.

  13.        The strncat() function is similar, except that it will use  at  most  n
  14.        characters  from src.  Since the result is always terminated with `\0',
  15.        at most n+1 characters are written.

  16. RETURN VALUE
  17.        The strcat() and strncat() functions return a pointer to the  resulting
  18.        string dest.

  19. CONFORMING TO
  20.        SVID 3, POSIX, BSD 4.3, ISO 9899

  21. SEE ALSO
  22.        bcopy(3),  memccpy(3),  memcpy(3),  strcpy(3),  strncpy(3),  wcscat(3),
  23.        wcsncat(3)

  24. GNU                               1993-04-11                         STRCAT(3)
复制代码
回复 支持 反对

使用道具 举报

发表于 2005-1-25 12:20:16 | 显示全部楼层
Post by luddy2
i have "Sagmentation fault" error at strcat(), too

i don't know why , anyone can help me?

here is my code

[php]
#include <string.h>
#include <stdlib.h>

int main() {
  char* str="Hallo";
  char* ch="you";
  char* result=(char*)malloc(sizeof(char)*20);
  printf("%s\n", str);
  printf("%s\n", ch);
  result=strcat(str, ch);  //have error this line
  printf("%s\n", result);
  return 0;
}

[/php]

错得一塌糊涂。不光错在这一行。还会有一个memory leak。你是一点也不了解C语言。你还是先去仔细看一下C语言的基础教程吧。
回复 支持 反对

使用道具 举报

发表于 2005-1-25 17:29:35 | 显示全部楼层
也不能说是错的一塌糊涂,谁写程序敢保证不出错误呢?错了就给人家指出来嘛!
"Hallo","You"都是放到数据段.
str和ch都是一个字符指针,在堆栈中只分配4个字节。
用不着动态分配内存!
可以考虑dest用一个array!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表