| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 5172 人关注过本帖
标题:myajax95斑竹,我要求你把那个帖子解开
只看楼主 加入收藏
热情依然
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:22
帖 子:715
专家分:0
注 册:2005-4-5
收藏
得分:0 
以下是引用穆扬在2006-7-24 23:54:46的发言:

谢谢kai

我完全了解指针(或者说是某斑竹所说的c语言的“简单指针”)
我不清楚的是c++部分

我的问题是,如果
int *p = new int[10];
那么
(new int[10]) +1 是否是合法的运算
如果是
(new int[10]) 与 (new int[10]) +1 相差几个字节

你不用问了,我都回复了,要是想认证,请下载编译器


c++/C + 汇编 = 天下无敌
2006-07-25 00:06
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 
穆扬,

when the place is allocated, then you can use it, otherwise you can not use it.

for example:

int * a = new int[10];

after last statement, you can use a[0] to a[9], but you can not use a[10], why? because, this place is not allocated, so it is not allowed.

now let's talk about this write syntax
(new int[10]) +1, such write syntax is ugly, make something confuse, but it is valid, let us look at a democode:

#include <iostream>
using namespace std;

int main()
{
int * a = (new int[5]) + 1;


for(int i = 0; i<5; i++)
{
*(a+i-1) = i;
cout<<a[i-1]<<" ";
}
return 0;
}



自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2006-07-25 00:07
穆扬
Rank: 1
等 级:禁止发言
帖 子:1910
专家分:0
注 册:2006-6-1
收藏
得分:0 
提示: 作者被禁止或删除 内容自动屏蔽

2006-07-25 00:09
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 
穆扬,

I think direct here answer is better than private mail, because everyone here can also take a look,

When you have any question, just ask, I will do my try to answer you.

now to your question above,

int *pi;
double *pd;
……
pi=pd;
在c++的编译器里是非法的还是仅仅给个警告

my answer is: it is not valid, that mean, you will get an error in compile time. The rease for that is, they have not same type. pi is from type pointer integer, but pd is from type pointer double, so pi = pd is error. you should use type cast, such as pi = (int *)pd; then it is ok.

自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2006-07-25 00:15
穆扬
Rank: 1
等 级:禁止发言
帖 子:1910
专家分:0
注 册:2006-6-1
收藏
得分:0 
提示: 作者被禁止或删除 内容自动屏蔽

2006-07-25 00:15
穆扬
Rank: 1
等 级:禁止发言
帖 子:1910
专家分:0
注 册:2006-6-1
收藏
得分:0 
提示: 作者被禁止或删除 内容自动屏蔽

2006-07-25 00:29
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 
穆扬,

you are welcome, anytime you can ask me question. I would like to answer you so long as I can.

Till now, we have just spoken about how to use new. Now I have a question for you, I can of course answer you, but I hope you can find the answer by yourself and tell me in some day later. The question is: why in c++ will new in stead of malloc used? or in other words, what is the advantage of using new? Think it over.

It is the time for you to learn c++ You need a good book, my suggestion for you is C++ Primer Plus Fifth Edition

Here I want tell you, it is very difficult to learn C++ for C programmer. Why difficult? because the mode of thinkting is different. I took one year to change my thoughmode from c to c++. And after learning java, I began to understood what is OOP. You will learn programming technique, but it is not all, the most import is thought about struct, about model, about object.

Now it is not the time to tell you too more, it is the time for you to start your way.

自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2006-07-25 00:33
穆扬
Rank: 1
等 级:禁止发言
帖 子:1910
专家分:0
注 册:2006-6-1
收藏
得分:0 
提示: 作者被禁止或删除 内容自动屏蔽

2006-07-25 00:36
穆扬
Rank: 1
等 级:禁止发言
帖 子:1910
专家分:0
注 册:2006-6-1
收藏
得分:0 
提示: 作者被禁止或删除 内容自动屏蔽

2006-07-25 00:40
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 
some declaretion about the democode:

#include <iostream>

using namespace std;


int main()
{
int * a = (new int[5]) + 1; // usually we write code like this int * a = new int[5];
// here new int[5]; is place allocation, for example the first address is 3000
// then (new int[5]) + 1; is address 3004, why 3004 not 3001, because every int
// need 4 byte, so 3000 + 4 = 3004

// with this for loop with this pointer array initialized
for(int i = 0; i<5; i++)
{
*(a+i-1) = i; // because a is not the first address, but the second, so we need minus 1, so that a - 1 is the first address
cout<<a[i-1]<<" ";
}

delete [] a; // it is very import, when you use new, then not forget use delete to frei the allocation.
// when allocation for an array, then delete [] name, otherwise delete name;
return 0;
}

(new int[5]) 开辟的内存会被初始化吗?还是垃圾值?
我感觉您的例子无法说明我那个问题
而且这样是否存在所谓的“指针泄露”问题呢?

To your question, you are right, before return 0; we should insert a statement, delete [] a; otherwise there is space lack problem.

自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2006-07-25 00:46
快速回复:myajax95斑竹,我要求你把那个帖子解开
数据加载中...
 
   



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

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