内联函数如何公开内部数据结构?
我多次听到这样的说法:“C 中的内联函数暴露了内部数据结构”,这就是有些人不喜欢它们的原因之一。
有人可以解释一下吗?
提前致谢。
假设我有一个程序 code.c 和一个函数 func()。我可以 1)使 func() 内联 - 这将公开我对 code.c 中的数据结构所做的任何操作 2)我可以将 func() 放入库中并将其作为共享库提供(这是不可读的 - 我猜猜??:p) ---- 这是正确的分析吗?
I hear this a lot of times that: "inline functions in C expose internal data structures" and that is one of the reasons some people do not like them.
Can someone please explain, how?
Thanks in advance.
Lets say I have a program code.c and a function func(). I can 1) make func() inline - which will expose whatever I do with my data-structures in code.c 2) I can put func() in a library and provide that as a shared lib (which is not readable - I guess ?? :p) ---- Is this a correct analysis?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于您将内联函数定义放在头文件中(除非在单个 cpp 文件中使用),而消费者需要将其包含在内,那么我猜您正在暴露代码的内部工作原理。
但是,由于替代方案通常是宏,我怀疑这是反对它们的充分理由。
Since you put inline function definitions in a header file (unless used in a single cpp file), which would need to be included by consumers then I guess you are exposing the inner workings of your code.
But, since the alternative is usually macros, I doubt that is a good reason against them.
与编译到库或对象模块中的内容相比,它肯定会更加透明。这是因为您可以查看源代码,因此可以编写以任何您想要的方式操作数据结构的代码。
但是,对于您拥有源代码的非线性函数,我不知道如何更好地保护它。
有些软件公司小心翼翼地保护他们的软件源代码,并且只发布要链接的对象模块、共享库或(可怕的!).DLL。
It would certainly be more transparent compared to something compiled into a library or object module. That's because you can see the source code, and therefore write code which manipulates the data structures any way you want.
However, for non-line functions for which you have source, I am at a loss how that could be more protected.
There are software corporations which jealously guard their software source code, and only release object modules to be linked with, or shared libraries, or (dread!) .DLLs.
内联方法将所有方法调用就地展开。因此,foo() 不是 JMP 或 CALL 指令,它只是复制调用它的 foo() 的实际指令。如果其中包含关键数据,那么尽管内联函数通常用于较短的一到两行方法或较大的表达式,但该数据仍将被暴露。
Inline methods expand all method calls in place. So instead of having foo() be a JMP or CALL instruction it just copies the actual instructions of foo() where it was called. If this contains critical data then that would become exposed although inline functions are typically used for short one to two line methods or larger expressions.