| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 498 人关注过本帖
标题:一个动态分配的内存地址的疑问
只看楼主 加入收藏
loriwang
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2007-8-24
收藏
 问题点数:0 回复次数:6 
一个动态分配的内存地址的疑问

程序代码如下:
#include <iostream>
using namespace std;
int main()
{
char* ca=new char[100];
cout<<ca<<endl;

int* i=new int[100];
cout<<i<<endl;

delete []ca;
delete []i;

}

程序输出的结果 ca的值为一个方片和一个红心,就是ascii中04和03,i的值是正常的一个16进制值0x32540.请问对于ca的值,是程序逻辑有问题还是输出结果的问题?

搜索更多相关主题的帖子: 内存 疑问 动态 地址 
2007-09-18 21:57
雨中飞燕
Rank: 3Rank: 3
等 级:禁止访问
威 望:8
帖 子:2200
专家分:0
注 册:2007-8-9
收藏
得分:0 
因为ca指向的东西没有初始化



by 雨中飞燕 QQ:78803110 QQ讨论群:5305909

[url=http://bbs.bc-cn.net/viewthread.php?tid=163571]请大家不要用TC来学习C语言,点击此处查看原因[/url]
[url=http://bbs.bc-cn.net/viewthread.php?tid=162918]C++编写的Windows界面游戏[/url]
[url=http://yzfy.org/]C/C++算法习题(OnlineJudge):[/url] http://yzfy.org/
2007-09-18 22:02
loriwang
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2007-8-24
收藏
得分:0 

ca保存的不是new char[100]返回的内存地址么?为什么说没有初始化.不过我试了下以下几个语句的输出
char* ca=new char[100];
ca[0]='a';
cout<<ca<<endl;
输出为a方片红心
char* ca=new char[100];
ca[0]='a';
ca[1]='b';
cout<<ca<<endl;
输出为ab红心
char* ca=new char[100];
ca[0]='a';
ca[1]='b';
ca[2]='c';
cout<<ca<<endl;
输出为abc
难道<<自动输出指针指向的地址内容?


Nothing is everything
2007-09-18 22:32
雨中飞燕
Rank: 3Rank: 3
等 级:禁止访问
威 望:8
帖 子:2200
专家分:0
注 册:2007-8-9
收藏
得分:0 
除了字符指针,其它都是直接输出指针地址
如果不是这样的话,那么:
char str[]="hello world";
cout<<str;
你难道希望它输出str的地址吗??



by 雨中飞燕 QQ:78803110 QQ讨论群:5305909

[url=http://bbs.bc-cn.net/viewthread.php?tid=163571]请大家不要用TC来学习C语言,点击此处查看原因[/url]
[url=http://bbs.bc-cn.net/viewthread.php?tid=162918]C++编写的Windows界面游戏[/url]
[url=http://yzfy.org/]C/C++算法习题(OnlineJudge):[/url] http://yzfy.org/
2007-09-18 22:45
loriwang
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2007-8-24
收藏
得分:0 
明白了,谢谢.

Nothing is everything
2007-09-18 22:48
HJin
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:401
专家分:0
注 册:2007-6-9
收藏
得分:0 
回复:(loriwang)一个动态分配的内存地址的疑问

#include <iostream>
using namespace std;
int main()
{
char* ca = new char[4];
cout << ca << endl; // cout content of ca[] until it reaches a '\0' char
printf("%p\n", ca); // prints ca's address in the heap

int* i = new int[4];
cout << i << endl;
/** heap grows up: 00383538 < 00383548.

You see there is some unused bytes between the two variables.
This is a disadvantage of dynamic allocation --- you may create
a lot of fragments in the heap memory.
*/

/** output:

X8
00383538
00383548
0012FF6C
0012FF68
Press any key to continue . . .
*/

// stack grows down: 0012FF6C > 0012FF68
// both i and ca live on the program stack
cout << & ca << endl;
cout << & i << endl;

delete [] ca;
delete [] i;

}


I am working on a system which has no Chinese input. Please don\'t blame me for typing English.
2007-09-18 23:21
loriwang
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2007-8-24
收藏
得分:0 
Thanks Hjin. I have got the key. If I want to print a char variable's address then cast char* to void*.

Nothing is everything
2007-09-18 23:41
快速回复:一个动态分配的内存地址的疑问
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.011826 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved