what is __dirstream, where can we find the definition

跟風遠走 提交于 2019-12-23 09:47:17

问题


As we can see in dirent.h, there is a typedef:

typedef struct __dirstream DIR;

It says it's opaque to users. And we can't find it even in gcc headers, the __dirstream structure definition can only be found in glibc source code. I am wondering why this structure has to be opaque (I don't think it's plausible to say it prevents user from direct reference, probably I am wrong)? What's the catch?


回答1:


You can find the actual definition here, but as has been pointed out you're not supposed to access it directly.

It can change randomly between implementations of the standard library (or different releases of the same library) which is why it's hidden.

Your comment about "informing the user" being enough is a bit ... I don't know, it's a very optimistic outlook. Hiding the definition makes it harder to abuse it, which in turn makes it harder to write programs that violate the specification and contain pointlessly brittle code. It's a good thing; you'll find a lot of software engineering is dedicated to trying to prevent programmers from doing what they're not supposed to be doing.

This in turn is not necessarily because "all programmers are stupid", at least that's not my interpretation, it's rather since the complexity of typical software systems is very, very high, it becomes beneficial to try to reduce it whenever possible. Information hiding is one way of reducing the complexity at one particular place in the system.



来源:https://stackoverflow.com/questions/16893895/what-is-dirstream-where-can-we-find-the-definition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!