免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 2628 | 回复: 11
打印 上一主题 下一主题

帮我看下这几个语句(char *的问题),谢谢! [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-10-10 13:30 |只看该作者 |倒序浏览
#include <string.h>
#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
        int n=0;
        const char *testlen="This string was created to test the function strpy()....end";
        cout<<testlen<<endl;
        cout<<"Length is:        "<<strlen(testlen)<<endl;

        char *newstring=new char(strlen(testlen));
       
        strcpy(newstring,testlen);
       
       
        cout<<"This is the new string:\n"<<newstring<<endl;
       
        return 0;
}


使用vc7或者cygwin的g++编译都没有问题,但执行时,在vc7中会引起异常,在g++中不会有异常,但结果不对,把char *newstring=new char(strlen(testlen));语句分配的长度+1也不行,不知道问题在哪里.....,谢谢!

论坛徽章:
0
2 [报告]
发表于 2006-10-10 13:36 |只看该作者
char *newstring=new char(strlen(testlen)+1);

论坛徽章:
0
3 [报告]
发表于 2006-10-10 13:37 |只看该作者
永远记住字符串,字符数组的边界检查
越界是常有的事情

论坛徽章:
0
4 [报告]
发表于 2006-10-10 13:38 |只看该作者
原帖由 freshstart 于 2006-10-10 13:30 发表
#include <string.h>
#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
        int n=0;
        const char *testlen="This string was created to test the functi ...


我没看全,不过一眼扫过去,第一反应是
char *newstring=new char(strlen(testlen)); ==>
char *newstring=new char(strlen(testlen) + 1);

论坛徽章:
0
5 [报告]
发表于 2006-10-10 13:43 |只看该作者
C:\MinGW\bin>a
This string was created to test the function strpy()....end
Length is:        59
This is the new string:
This string was created to test the function strpy()....end

论坛徽章:
0
6 [报告]
发表于 2006-10-10 13:47 |只看该作者
因为刚才一看LZ说+1也不行,特意在Linux下再做测试如下:

[root@linux wuqing]# more testcplus.cpp

  1. #include <string.h>
  2. #include <stdio.h>
  3. #include <iostream>
  4. using namespace std;
  5. int main()
  6. {
  7.         int n=0;
  8.         const char *testlen="This string was created to test the function strpy()....end";
  9.         cout<<testlen<<endl;
  10.         cout<<"Length is:        "<<strlen(testlen)<<endl;

  11.         char *newstring=new char(strlen(testlen)+1);
  12.       
  13.         strcpy(newstring,testlen);
  14.       
  15.       
  16.         cout<<"This is the new string:\n"<<newstring<<endl;
  17.       
  18.         return 0;
  19. }
复制代码


[root@linux wuqing]# g++ -o testcplus testcplus.cpp
[root@linux wuqing]# ./testcplus
./testcplus: /usr/local/staf/lib/libstdc++.so.6: no version information available (required by ./testcplus)
./testcplus: /usr/local/staf/lib/libstdc++.so.6: no version information available (required by ./testcplus)
This string was created to test the function strpy()....end
Length is:        59
This is the new string:
This string was created to test the function strpy()....end
[root@linux wuqing]#

论坛徽章:
0
7 [报告]
发表于 2006-10-10 13:55 |只看该作者
谢谢!
我在那两种环境下又试了下,错误还是一样。。。
晚上回去在linux下试试再说,按说strlen(testlen)+1了以后应该没有问题了.....
难道是编译环境的问题??..
谢谢答复。

论坛徽章:
0
8 [报告]
发表于 2006-10-10 14:02 |只看该作者
char *newstring=new char(strlen(testlen));
是不是应该用
char * newstring = new char[strlen[testlen)+1]; ?

论坛徽章:
0
9 [报告]
发表于 2006-10-10 14:04 |只看该作者
原帖由 namtso 于 2006-10-10 14:02 发表
char *newstring=new char(strlen(testlen));
是不是应该用
char * newstring = new char[strlen[testlen)+1]; ?

非常感谢!犯了一个基本错误......

论坛徽章:
0
10 [报告]
发表于 2006-10-10 14:18 |只看该作者
原帖由 namtso 于 2006-10-10 14:02 发表
char *newstring=new char(strlen(testlen));
是不是应该用
char * newstring = new char[strlen[testlen)+1]; ?



谢谢指正.看代码不仔细.经过测试,发现错误的代码其实在各种平台下都可以编译通过,也可以运行正常(即使如LZ所说,不+1).

看来以后编译的时候一定要把警告级别提高
虽然能够编译通过,其实编译器还是可以提供很多信息的


------ 已启动全部重新生成: 项目: cccc, 配置: Debug Win32 ------

正在删除项目“cccc”(配置“Debug|Win32”)的中间文件和输出文件。
正在编译...
stdafx.cpp
正在编译...
cccc.cpp
d:\Development\cccc\cccc.cpp(16) : warning C4267: “参数” : 从“size_t”转换到“unsigned int”,可能丢失数据
d:\Development\cccc\cccc.cpp(1 : warning C4267: “初始化” : 从“size_t”转换到“char”,可能丢失数据
正在链接...

生成日志保存在“file://d:\Development\cccc\Debug\BuildLog.htm”中
cccc - 0 错误,2 警告


---------------------- 完成 ---------------------

    全部重新生成: 1 已成功, 0 已失败, 0 已跳过
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP