1

我使用嵌入式 JavaScript 引擎,我曾经将结果fopen()作为数字传递给 JS 代码。

int numeric_handle = (int)fopen(*P1, "rb");
return Number::New(numeric_handle); // Return to JavaScript

但是现在我需要在 64 位系统中重新编译,我得到了

fatal error: cast from pointer to smaller type 'int' loses information

现在我有点卡住了,我如何保持我的系统设计但尽可能少地更改代码?我正在考虑拥有一个哈希映射,一个包含 FILE* 并在该数组中返回一个 ID 的数组。但这将意味着额外的内存管理,额外的查找。

4

3 回答 3

2

long 应该足够了,但您也可以使用字节缓冲区

FILE *f;
char buffer[sizeof(f)];
memcpy(buffer, &f, sizeof(f));

为什么你需要这样的黑客?

于 2014-01-16T07:15:39.280 回答
1

使用指针大小的整数,例如 cstdint 中的 intptr_t。

于 2014-01-16T07:21:47.280 回答
0

我当然会选择“隐藏实际类型”,换句话说,使用 amapvector一些类似的类型来存储FILE *,并传递 theindex或任何需要的东西来检索FILE *需要它的调用。这样,您如何打开文件就无关紧要了(fstream例如,您以后可能想要使用 C++,这将更难存储为某种副本)。

于 2014-01-16T07:31:59.567 回答