递增函数指针 [英] Incrementing function pointers
问题描述
我刚刚了解函数指针(指针在哪里哪里函数的机器code存储在ADRESS指点)。这让我想到机code和它是如何存储在内存中。
I just learned about function pointers (pointers pointing at the adress where where the machine code of a function is stored). This made me think about machine code and how it is stored in memory.
时的连续存储在内存中的机器code,这样就可以手动提高指针,直到它指向以下/ previous功能?
Is the machine code stored consecutively in memory, so that it is possible to "manually" increase the pointer until it points to the following/previous function?
这是什么一个调试器呢?他让我看,其中程序计数器在机床code指点?
Is this, what a debugger does? He lets me "see" where the program counter is pointing in the machine code?
结论:人们可以用函数指针编写一个原始的调试器
Conclusion: one can program with function pointers a primitive debugger?
我才明白这个正确的,还是我的方法了吗?
Did I understand this right, or am I way off?
推荐答案
类。您正在承担的功能将在内存中奠定了他们在源$ C $ C以同样的方式。最有可能的,他们不会 - 编译器通常将它们移动周围的所有无可奈何地
Kind of. You are assuming functions will be layed out in memory the same way they are in the source code. Most likely, they will not be - the compiler usually moves them around all willy-nilly.
您的有什么能的事,但是,是一步通过code的指针当前指令,并通过一定量的递增那个柜台去下一个指令。然而,在这种情况下,我们将不再叫它的函数指针的,因为它不只是指向一个函数的开始;相反,我们就会把它称为指令指针。
What you could do, however, is step through the code with a pointer to the current instruction, and increment that counter by a certain amount to get to the next instruction. However, in that case we would no longer call it a function pointer, since it's not just pointing to the beginning of a function; instead, we'd call it an instruction pointer.
在事实上,这是一台电脑究竟是如何工作的 - 它有一个特殊的注册称为程序计数器总是指向当前的指令,每一个指令后递增它由一定量的(A GOTO
命令等同于将值写入程序计数器)的
In fact, this is exactly how a computer works - it has a special register called the program counter which always points to the current instruction, and increments it by a certain amount after every instruction (a GOTO
command is equivalent to writing a value into the program counter).
在现实世界中,然而,这不是调试器如何工作 - 事实上,我甚至不知道是否有可能有一个指针指向code级在内存中以C,除函数指针的。更有可能的是,你只需要使用这个技术,如果你需要模拟一个程序计数器,比如写一个仿真器用于其他处理器类型。
In the real world, however, this is not how debuggers work - in fact, I'm not even sure if it's possible to have a pointer point to the code-segment in memory in C, other than a function pointer. More likely, you would only need to use this technique if you needed to simulate a program counter, such as writing an emulator for another processor-type.
这篇关于递增函数指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!