gpt4 book ai didi

带指针的C程序

转载 作者:行者123 更新时间:2023-11-30 21:30:04 25 4
gpt4 key购买 nike

是否可以将任何使用指针编写的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com