在64位机器上处理文件但在32位机器上开发
我将在 64 位 Windows 机器上使用 C 语言的 Mexfunction 在 matlab 中读取 TDMS 文件,但我将在 32 位 Windows 机器上开发该应用程序。我知道 32 位机器和 64 位机器之间存在变量大小的差异。我使用了很多 fread(.. sizeof(type)..)。在64位机器上运行会出现问题吗?如果是这样,我怎样才能让它移植到 64 位机器?
谢谢
i am going to read a TDMS file in matlab using Mexfunction in C language in a 64 bit windows machine, but i will develop the app in 32 bit windows machine. i know in there is a difference between 32 bit machine and 64 bits with the size of variables. i used a lot of fread(.. sizeof(type)..). is it going to be a problem when it is running in 64 bit machine? if so, how can i make it portable to 64 bits mahince?
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
ISO C99 提供了标头,其中定义了 intN_t 和 uintN_t 形式的类型,其中 N 是相应整数或无符号整数类型的宽度。如果实现提供宽度为 8、16、32 或 64 的整数类型,则它应该提供相应的 typedef。
ISO C99 provides the header <stdint.h>, which defines, amongst others, types of the form intN_t and uintN_t, where N is the width of the corresponding integer or unsigned integer type. If an implementation provides integer types of width 8, 16, 32 or 64, it should provide the corresponding typedefs.
更普遍的问题是,您必须知道写入文件的机器上变量的大小,而不是读取文件的机器上的变量的大小。换句话说,在某些疯狂的 64 位系统上,您可以说 sizeof(int) 并得到 8,但如果文件保存在普通的 32 位计算机上,则 sizeof(int) 可能是 4(甚至 2,根据 ansi c,我认为)。 sizeof 命令将告诉您编译时本地计算机上 int 或其他内容的大小。但它无法告诉您有关保存文件的计算机的任何信息。
您最好的选择是查看 TDMS 标准(我不熟悉)是否定义了可变大小。如果是这样,您应该使用它们,而不是 sizeof。
第二种糟糕的选择是在文件开头有一个测试序列,并动态调整变量大小,直到可以正确读取测试序列。
The more general problem is that you will have to know what the size of the variables were on the machine that WROTE the file, not the machine that is reading them. In other words, you can say sizeof(int) and get say 8 on some crazy 64 bit system, but if the file was saved on a normal 32 bit machine, sizeof(int) may be 4 (or even 2, according to ansi c, I think). The sizeof command will tell you the size of an int, or whatever, on your local machine at the time of compile. But it can't tell you anything about the machine that saved the file.
Your best bet is to see if the TDMS standard (I'm not familiar with it) defines variable sizes. If so, you should use those, rather than sizeof.
A poor second alternative is to have a test sequence at the beginning of the file and dynamically adjust your variable sizes until you can read the test sequence correctly.
是的,根据您的操作,可能会出现问题。例如,如果您依赖 4 字节或 8 字节的指针大小,这将是一个问题。然而,如果你正在做一些良性的事情,那么也许就不会。我想我们必须看到具体的代码才能告诉你。简而言之,应该有一种简单的方法来解决这个问题,而不必关心您是否处于 64 位或 32 位体系结构中。
Yes, there could potentially be an issue depending on what you do. For instance, if you rely on pointer size being 4 bytes or 8 bytes, this will be an issue. However, if you are doing something benign than maybe not. I think we'd have to see the specific code to be able to tell you. In short, there should be a straightforward way to go about this without caring about whether or not you are in a 64-bit or 32-bit architecture.