带指针的C程序

标签 c pointers

是否可以将任何使用指针编写的 C 程序转换为另一个不包含任何指针的 C 程序?如果可以,我们可以自动化该过程吗?

我读了一些关于c到java字节码编译的论文,发现一个主要问题是“指针问题”。所以我在想,如果上面的过程可以完成,那么它可以像预处理步骤一样包含在内(尽管它本身可能是一项艰巨的任务),然后尝试转换为 jvm 字节码可能会更简单......

提前致谢

最佳答案

理论上,您可以通过使用数组模拟单个数据结构甚至整个内存(静态数据、堆和堆栈)。但问题是这是否非常实用?它可能涉及必须重写您需要的每个基于指针的标准库函数。

无论如何,Wikipedia 有一个很好的解释:

It is possible to simulate pointer behavior using an index to an (normally one-dimensional) array.

Primarily for languages which do not support pointers explicitly but do support arrays, the array can be thought of and processed as if it were the entire memory range (within the scope of the particular array) and any index to it can be thought of as equivalent to a general purpose register in assembly language (that points to the individual bytes but whose actual value is relative to the start of the array, not its absolute address in memory). Assuming the array is, say, a contiguous 16 megabyte character data structure, individual bytes (or a string of contiguous bytes within the array) can be directly addressed and manipulated using the name of the array with a 31 bit unsigned integer as the simulated pointer (this is quite similar to the C arrays example shown above). Pointer arithmetic can be simulated by adding or subtracting from the index, with minimal additional overhead compared to genuine pointer arithmetic.

关于带指针的C程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28095882/

相关文章:

c++ - 当使用 'new int[n]' 声明一个新数组时,为什么它没有出现在本地列表中?

C 从 char 指针一点一点地读取

c - 使用缓冲区溢出攻击修改返回地址

c++ - 四元数和翻译

c - 保证 int32_t 和 float 之间的数据保存?

c - 指向函数的函数指针返回指向所述函数类型的指针

c - C语言编程中的二分法

javascript - 更新 Javascript 二维数组中的元素会更新整个列

c - 更新c中的动态数组

c - 使用指针查找重复项