LinuxSir.cn,穿越时空的Linuxsir!

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

问题虽简单但值得问透彻了解

[复制链接]
发表于 2006-4-11 12:41:47 | 显示全部楼层 |阅读模式

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>

  4. int main(void)
  5. {
  6.     char* iconfilename[20];
  7.     char* iconfile = " I very very love motherland";
  8.   
  9.     memcpy(iconfilename, iconfile, strlen(iconfile)+1);
  10.     fprintf(stdout, "%s\n", iconfilename);

  11.     strncpy(iconfilename, iconfile, 20);
  12.     fprintf(stdout, "%s\n", iconfilename);

  13.     strcpy(iconfilename, iconfile);
  14.     fprintf(stdout, "%s\n", iconfilename);

  15.    
  16.     return 0;
复制代码

这几种的拷贝那个比较安全,那个危险,为什么?
发表于 2006-4-11 13:00:45 | 显示全部楼层
这三种方法都是不安全的。
第一种可能导致Segmentation fault
第二种iconfilename字符串因为可能没有'\0'结束符,用fprintf输出时导致输出的内容比iconfilename[]数组存储的内容还要多。
第三种可能导致Segmentation fault
回复 支持 反对

使用道具 举报

发表于 2006-4-11 14:01:51 | 显示全部楼层
第二个,拷贝完把[20]赋值为'\0'就没事了
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-4-11 15:14:57 | 显示全部楼层
如果这样的都安全了吧

  1. char iconfile[] = " I very very love motherland";

复制代码

strcpy的des要足够大,否则内存溢出?
回复 支持 反对

使用道具 举报

发表于 2006-4-11 15:17:37 | 显示全部楼层
你试试能编译通过么
回复 支持 反对

使用道具 举报

发表于 2006-4-11 16:06:31 | 显示全部楼层
Post by zhllg
第二个,拷贝完把[20]赋值为'\0'就没事了

呵呵,应该是[19].
Post by sybaselu
如果这样的都安全了吧

  1. char* iconfile[] = " I very very love motherland";
复制代码

char* iconfile = " I very very love motherland";这条语句是安全的。只是下面的拷贝语句不安全。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-4-12 14:00:22 | 显示全部楼层

  1. int main(void)
  2. {
  3.     char iconfilename[20];
  4.     char* iconfile = "I very very love aaahaa";

  5.     memcpy(iconfilename, iconfile, 10);
  6.     printf("%s\n", iconfilename);


  7.     strncpy(iconfilename, iconfile, 10);
  8.     printf("%s\n", iconfilename);

  9.     return 0;
  10. }
  11. [root@root GUI]# gcc PointerIni.c -o PointerIni -Wall
  12. [root@root GUI]# ./PointerIni
  13. I very ver泾j

  14. I very ver泾j
  15. [root@root GUI]# vi PointerIni.c
  16. ...
  17. int main(void)
  18. {
  19.     char iconfilename[20];
  20.     char* iconfile = "I very very love aaahaa";

  21.     memcpy(iconfilename, iconfile, 10);
  22.     fprintf(stdout, "%s\n", iconfilename);


  23.     strncpy(iconfilename, iconfile, 10);
  24.     fprintf(stdout, "%s\n", iconfilename);

  25.     return 0;
  26. }
  27. "PointerIni.c" 20L, 345C written                              
  28. [root@root GUI]# gcc PointerIni.c -o PointerIni -Wall
  29. [root@root GUI]# ./PointerIni
  30. I very ver帼

  31. I very ver帼

  32. [root@root GUI]#

复制代码

对memcpy()来说,拷贝字符串时多少个字节和多少个character没有区别,但memcpy()用来拷贝非char时,就要注意num of bytes
回复 支持 反对

使用道具 举报

发表于 2006-4-12 14:07:36 | 显示全部楼层
Post by ideawu
呵呵,应该是[19].

笔误,了解我的意思就好了,呵呵
反正就是加个'\0'作为结束
Post by ideawu

char* iconfile = " I very very love motherland";这条语句是安全的。只是下面的拷贝语句不安全。

但是
char* iconfile[] = " I very very love motherland";
是错的
回复 支持 反对

使用道具 举报

发表于 2006-4-12 14:10:33 | 显示全部楼层
  1. #include<stdio.h>
  2. #include<string.h>
  3. int main(void)
  4. {
  5.     char iconfilename[20];
  6.     char* iconfile = "I very very love aaahaa";
  7.     memcpy(iconfilename, iconfile, 10);
  8.     iconfilename[9] = '\0';
  9.     fprintf(stdout, "%s\n", iconfilename);
  10.     strncpy(iconfilename, iconfile, 10);
  11.     iconfilename[9] = '\0';
  12.     fprintf(stdout, "%s\n", iconfilename);
  13.     return 0;
  14. }
复制代码
回复 支持 反对

使用道具 举报

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

本版积分规则

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