嵌入式系统中的函数指针是否有用? [英] Function pointers in embedded systems, are they useful?

查看:173
本文介绍了嵌入式系统中的函数指针是否有用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在采访中,他们询问在编写嵌入式系统的代码时是否使用函数指针是有益的(在速度方面)?我不知道嵌入式系统,所以不能回答这个问题。只是一个多云或模糊的答案。
那么真正的好处是什么?速度,可读性,维护成本?

In an interview they asked me if using function pointers would be beneficial (in terms of speed) when writing code for embedded systems? I had no idea on embedded system so could not answer the question. Just a cloudy or vague answer. So what are the real benefits? Speed, readability, maintenance,cost?

推荐答案

我想也许Viren Shakya的回答错过了访问者试图引用的观点。在某些结构中,函数指针的使用可能会加快执行速度。例如,如果你有一个索引,使用它来索引一个函数指针数组可能比一个大的开关更快。

I think perhaps Viren Shakya's answer misses the point that the interviewer was trying to elicit. In some constructs the use of a function pointer may speed up execution. For example, if you have an index, using that to index an array of function pointers may be faster than a large switch.

然而,如果您正在通过一个指针比较一个静态函数调用和一个调用,那么Viren正确地指出有一个额外的操作来加载指针变量。但是没有人合理地尝试以这种方式使用函数指针(作为直接调用的替代方法)。

If however you are comparing a static function call with a call through a pointer then Viren is right in pointing out that there is an additional operation to load the pointer variable. But no one reasonably tries to use a function pointer in that way (just as an alternative to calling directly).

通过指针调用函数不是一种替代方法直接通话。所以,优势的问题是有缺陷的;它们在不同的情况下使用,经常简化其他代码逻辑和控制流程,而不仅仅是避免静态函数调用。它们的用处在于,通过代码而不是由链接器静态地在运行时动态地执行要调用的函数的确定。在这个意义上来说,它们在嵌入式系统中当然是有用的,但并不是因为与嵌入式系统有关的任何原因。

Calling a function through a pointer is not an alternative to a direct call. So, the question of "advantage" is flawed; they are used in different circumstances, often to simplify other code logic and control flow and not to merely avoid a static function call. Their usefulness is in that the determination of the function to be called is performed dynamically at run-time by your code rather than statically by the linker. In that sense they are of course useful in embedded systems but not for any reason related to embedded systems specifically.

这篇关于嵌入式系统中的函数指针是否有用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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