标题:[求助]2重指针的问题-->myajax95转移-->baidu转移
只看楼主
wfpb
Rank: 6Rank: 6
等 级:贵宾
威 望:29
帖 子:2188
专家分:0
注 册:2006-4-2
得分:0 

最喜欢偷笑的人就是你了,老是


[glow=255,red,2]wfpb的部落格[/glow] 学习成为生活的重要组成部分!
2006-07-24 12:29
热情依然
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:22
帖 子:715
专家分:0
注 册:2005-4-5
得分:0 
以下是引用穆扬在2006-7-24 12:22:28的发言:

我记得c的精神是信任程序员,不给他们限制
看来c++不是这样

还有
int *pi;
double *pd;
……
pi=(int *)pd;在c++中应该没问题吧

一样会报错,C++编译器不是愚蠢的


c++/C + 汇编 = 天下无敌
2006-07-24 12:31
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
得分:0 
linag wei,

wo ji ben kan guo le suo you de tie zhi le.

一、
int*p;
int arr[10][10];
p=arr; // pay attention here, here is wrong, why? because the type is not same, p is type from int * and arr is type from
2 dimensional array, so they are not same type.
//改成p=&arr[0][0]; // but here is right, why? let us take a look, arr[0][0] is an integer invalue, and with & will get the address of this value, so &arr[0][0] is an address of an integer value, and p is pointer to integer so they are same type.

二、
int *p[10];
for(int i=0;i<10;i++)
p[i]=new int[10];
for(int j=0;j<10;j++)
for(int k=0;k<10;k++)
p[j][k]=arr[j][k];

Method 1 is not right, because one dimension array is not same as 2 dimensional array. In Programming you can use one dimensional array to work with 2 dimensional array, but not encourage.

Method 2 is total right.
first look hier int * p[10]; this declartion tell the compiler this is an pointer array and in type integer, so the compiler will allocate 40Byte for this pointer array. pay attention here, after declartion, the space is already allocated, that mean, this 40 Byte is there.
now the program use a for loop and p[i] = new int[10]; here compiler will allocate 40Byte again for every p[i], and after allocation the address will be assigned to p[i]. Let's say something concrrect, first time the allocation is not dynamica, it is static, it is just like int a; here a is also has a place to receive his value. And here int * p[10]; the place is also static, suppose the first address is 2000, then the second is 2004 and so on. Now after p[i]=new int[10]; for example p[0] get the first address for this allocation is 3000, then this 3000 will be saved in the space which has the address 2000, and the other is same.

and this last two for loop is just for initialization.
for(int j=0;j<10;j++)
for(int k=0;k<10;k++)
p[j][k]=arr[j][k];

so it is correct.

Now to some other subject, 穆扬. you have said something about type convertion.
for instance:
int * p = new int;
double * pd = new double;
pd = (double *)p; // it is correct, why? because, I have casted the type to the same type.
pd = p; // here is of course not right, why? because they are not tpye.

I think, my personal meaning,
穆扬 is a little more than normal discuss. wfpb has well done. So I think 穆扬 you should change your attitute. Discussion is just discussion, every one can say your meaning. Even your meaning is wrong. It is also ok, the important thing is, after discussion, you should learned something, and you need not care, whether some one else also learned something, you are not teacher, I am also not, So it is not semeter exam. You need just look at yourself, you need just take care of yourself.

And, and this is also but, you can, and you also should keep your meaning, keep your mind, when you are not sure, whether the other ones are right, or when you still not understand, then please ask the question, till you get the meaning. That is learning.

Through this discussion, I hope
穆扬 and wfpb never keep attack to each other. I think, you are both good boy. Why not friend? I hope so.

C is a subset for C++, that is right, but it doesn't mean, C++ is just C, or otherwise, C is just C++. No, these 2 things has very different philosophy. My personal meaning, c++ is a language for freedom. Why freedom? not understand? Here freedom does mean, someone can use C++ implement his thought, his logic. You should know, the human being is different, they have total different thought model. That mean, the aspect of viewing of a problem can be huge different. And with c++ you can implement your thought without any restriction. Here I have not spoken anything about effiencient. First the freedom for thought should be protected, this is very important, because the development in multidimension is better than its in one dimension. Only so, the technic will be improved in a compromise wise.

For a c++ programmer, he look the the world is real, that mean, all are object, the real world is just a set of Objects, and the system is some objects with their relation. So solving a problem is just the simulation der real world. And meanwhile, c programmer see the solving of a program is a chain of the solving of subproblem. So you see, the aspect is different.

Words till now.

To technic problem, anyone can still make discussion continue. And I will also join us.



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

2006-07-24 12:42
热情依然
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:22
帖 子:715
专家分:0
注 册:2005-4-5
得分:0 
kai, your english is very well, recently, I'm tryied my best to look up msdn without "金山词霸"

c++/C + 汇编 = 天下无敌
2006-07-24 12:44
穆扬
Rank: 1
等 级:禁止发言
帖 子:1910
专家分:0
注 册:2006-6-1
得分:0 
提示: 作者被禁止或删除 内容自动屏蔽

2006-07-24 12:48
baidu
Rank: 3Rank: 3
等 级:新手上路
威 望:8
帖 子:3811
专家分:0
注 册:2005-11-4
得分:0 
C++是因为继承了C所以才强大。

偶放弃所有文章版权,偶在BCCN论坛任何贴子,可转贴,可散发,可抄袭,可复制,可被冒名顶替,可被任何人引用到任何文章中且不写出引文出处,偶分文不取。
2006-07-24 12:49
热情依然
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:22
帖 子:715
专家分:0
注 册:2005-4-5
得分:0 
以下是引用穆扬在2006-7-24 12:42:00的发言:

规则多我可以理解
但是规则复杂且互相矛盾(至少看起来是这样)我觉得不能不说是一个语言的缺点
比如
int * = void * 可以
int * = double * 却不可以
为什么void *就特殊呢
在c看来各种指针都是平等的
我估计K&R一定了解并信奉佛教中众生平等的观念

C++这样做是防止以后内存访问出错,看来穆兄还不知道内存出错是多么可怕的事情.为了防止这些内存出错,内存泄漏,JAVA,C#都内置建立了一个垃圾收集器,来帮助程序员释放内存.所以操控C绝对比操控C++难,请穆兄要明白这点,不要以为限制少了,就可以方便,这是绝对危险的想法


c++/C + 汇编 = 天下无敌
2006-07-24 12:49
明天我依然
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2006-7-24
得分:0 
终于差不多从头到尾看完这个帖子了。
撇开c++的讨论,kai说的话很中听!(Your speech is very pleasant to the ear.Although nobody may be some person's teacher, but in your speech we could see you already to become our teacher. )
开个玩笑的说:“穆扬你啊,就是一个顽固的顶着小孩样的死老头”在技术上我们要求有你这样的严谨,但做人和讨论问题上需要的是自谦和谐和自我学习。

I am a roc!
2006-07-24 12:53
aiyuheng
Rank: 1
等 级:新手上路
威 望:1
帖 子:656
专家分:0
注 册:2006-1-12
得分:0 

用java吧 没显示的指针也自动回收垃圾


when i want to ask anyone,i will ask myself first.
2006-07-24 12:54



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-79710-1-1.html




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

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