指向指针的指针.那么我们能有多长时间 [英] Pointer to pointer to pointer. So how long can we have such chain

查看:63
本文介绍了指向指针的指针.那么我们能有多长时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

众所周知.我们可以有一个指向指针的指针..
但是在这里我想知道.到什么程度我们才能拥有这条链?

就像
指向指针的指针指向指针的指针...依此类推.


显然会有一些限制.但是那是什么,更重要的是为什么?


在此先感谢

解决方案

除了enhzflep的注释外,如果第一个指针指向第二个指针,而第二个指针指向第二个指针,它也很可能是无止境的.首先.

另外,在一个代码块中的长链中包含很多指针并不是很有用.但是,不同的库可以一起将指向函数的指针链接起来. Bios方法是一个很好的示例,因为在固定地址上有一个指针表,该表又指向可能因系统而异的bios例程.您的程序将获得指向该bios例程的指针的指针,但是很高兴的是,从您的代码角度来看,它只是一个指针. dll也是如此.这样,它可以在不中断程序的情况下重定位到任何位置(但是重定位表几乎从未使用过).对于纯数据对象/结构,链接很多指针是令人怀疑的,因为它没有帮助.某些重定向是有帮助的,但在某些时候它只是内存污染,根本就是不必要的.

祝你好运!


到什么程度,我们才能拥有这个链条?



好吧,答案是您可以随意设置一条链,直到其长度达到无法读取代码并被雇主解雇的程度."

超出2或3的链接指针是数据结构设计非常糟糕的标志.另外,当您的代码使用这种构造时,您实际上在显示向代码公开了结构的内部工作的程度.哦,我同意某些API,DLL和/或BIOS调用需要您执行这种操作,但实际上您不应该为自己的代码创建长链.

当您的问题是关于"C"而不是"C ++"时,应采用良好的编程习惯,并且应将指针的深度"封装在例程(在C中)或对象(在C ++中)中.

即使在C ++中,它也会变得很丑陋.如何在C ++中编写不良C的示例.


Hi Everyone,

As we all know. We can have pointer to a pointer to a pointer..
But here i would like to know. Till what extent we can have this chain?

like
pointer to a pointer to a pointer to a pointer ... so on.


Obviously there will be some limit. But What is that and more important is Why?


Thanks in advance

解决方案

In addition to the comment of enhzflep, It could also easily be endless if the first pointer points to a second, and this second pointer points back to the first.

Also, it isn''t very useful to have a lot of pointers in a long chain in just a single block of code. But different libraries together can chain up pointers to a function. Bios methods are a good example because there is a pointer table on a fixed address which again point to bios routines that might depend from system to system. Your program will get a pointer to the pointer of that bios routine, but the nice thing is that from your code''s perspective it''s just a single pointer. Same goes with dll''s by the way. This way it can relocate to any position without breaking the program (but the relocation table is almost never used). For pure data objects/structures it is doubtful to chain a lot of pointers because it doesn''t help. Some redirection is helpful but at a certain point its just memory pollution and simply unnecessary.

Good luck!


Till what extent we can have this chain?



Well, the answer is "you can have as long a chain as you like until it reaches such a length that the code is unreadable and your employer fires you".

Chaining pointers beyond 2 or 3 is a sign of really bad data structure design. Also, when your code uses such a construct, you are really showing how much you''ve exposed the inner workings of a structure to the code. Oh, I agree that some APIs, DLLs, and/or BIOS calls need you to do this kind of thing but you really shouldn''t be creating long chains for your own code.

While your question was about "C" and not "C++", good programming practices should apply and you should be encapsulating the "depth" of the pointers in a routine (in C) or an object (in C++).

And even in C++, it can get pretty ugly.

obj->GetCust()->GetAcct()->GetBalanceDue()

is just bad encapsulation and is a good example of how to write bad C in C++.


这篇关于指向指针的指针.那么我们能有多长时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆