相当于 F# 中的 size_t
我看到在 F# 中 List.length 的类型是 'a list -> int
是否可以安全地假设 int 始终足够大以包含我可能创建的任何列表的大小,这是因为列表仅限于 4G 项目吗?
我正在学习的书(Expert F# 2.0)说 int 和 int32 的大小相同,所以我开始使用 size_t 的 typedef 编写我的第一个程序。这是一个痛苦,因为它要求我明确告诉编译器我的意思是什么类型常量(例如 7UL 而不是 7),尽管我希望可以通过将 size_t 转换为具有 int 构造函数的对象来避免这种情况。我还预计这会导致一系列其他问题:-(
那么,通常的解决方案是什么?我是否只是在各处使用整数并忽略 4G 障碍?数组索引也是整数,所以我不能即使我应该能够很好地实现它以供使用,但我的 size_t 类型仍然非常有用
。
I see that in F# the type of List.length is 'a list -> int
Is it safe to assume that int will always be big enough to contain the size of any list I might create, and is that because lists are limited to 4G items?
The book I am learning from (Expert F# 2.0) says that int and int32 are the same size, so I set off writing my first program with a typedef for size_t. That is a pain because it requires me to explicitly tell the compiler what type constants I mean (e.g. 7UL instead of just 7), although I expect that could be avoided by turning size_t into an object with an int constructor. I also expect that would cause a range of other problems :-(
So, what is the usual solution please? Do I just use ints everywhere and ignore the 4G barrier? Array indices are ints too, so it's not like I would be able to do much useful with my size_t type even if I should be able to implement it well enough to use.
Many thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 .NET 中,您只能分配 2GB 的数组(即使您使用的是 64 位版本并且有足够的内存)。如果您确实想要存储这么多数据,那么您可能需要使用数组,因为 F# 列表的开销更大。
因此,您可能不需要担心长度限制为 32 位
int
。您唯一关心的情况是您是否正在编写数据结构来解决此 2GB 限制。In .NET, you can only allocate an array that has 2GB (even if you're using 64 bit version and have enough memory). If you actually wanted to store this ammount of data, then you'd probably want to use arrays, because F# lists have larger overhead.
For this reason, you probably don't need to worry about the length being limited to 32bit
int
. The only case where you would care is if you were writing data structure to workaround this 2GB limitation.一个 4G 整数数组需要 16GB RAM,而且这只适用于整数(而不是更复杂的东西)。除非你有那么多内存,否则没有理由关心这个限制。如果您确实想使用那么多内存,请考虑将数组拆分为 2-3 个数组。
An array of 4G ints, would require 16GB of RAM, and that's only for ints (and not anything more complex). Unless you have that much memory, there's no reason to care about that limit. If you do want to use that much memory, consider splitting you array into 2-3 arrays.
在 .NET 中,使用“int”表示集合大小。对于 99.999% 的情况来说它足够大,并且最好在所有 API 中都有标准类型。
In .NET, use "int" for collection sizes. It is big enough for the 99.999% case, and it is best to have standard types across all APIs.