size_t和off_t之间的用法区别是什么? [英] What are the usage differences between size_t and off_t?

查看:294
本文介绍了size_t和off_t之间的用法区别是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除了每种类型可以容纳的值的大小之外,size_toff_t之间的用法主要区别是什么?将size_t类型用于绝对大小而将off_t类型用于偏移量只是一种惯例吗?还是比这更深?

Other than the size of the values that each type can hold, what are the main differences in usage between size_t and off_t? Is it just a convention that size_t types are used for absolute sizes and off_t types are used for offsets? Or does it go deeper than that?

我正在编写一个包装器类,以允许使用mmap写入大型文件,并且我想知道什么是最好的参数类型.考虑到我要写入大于4GB的文件,我很想对所有内容都使用size_t,但这是最佳实践吗? (或者我应该为某些功能使用某些off64_t类型?)

I am writing a wrapper class to enable the writing of large files using mmap and I want to know what the best types are to use for their arguments. Given that I want to write to files > 4GB, I'm tempted to use size_t for everything, but is that the best practice? (or should I be using some off64_t types for certain functions?)

例如,我的writeAt函数应声明为:

For example, should my writeAt function be declared as:

MMapWriter::writeAt(off64_t offset, const void* src, size_t size)

MMapWriter::writeAt(size_t offset, const void* src, size_t size)

推荐答案

size_t用于对象,off_t用于文件.

size_t is for objects, off_t is for files.

mmap合并了这两个概念,这在定义上几乎是一样的.我个人认为我会使用size_t,因为无论它是什么,映射文件也是(虚拟)内存中的数组.

mmap merges the two concepts, pretty much by definition. Personally I think I'd use size_t, since no matter what else it is, a mapped file is also an array in (virtual) memory.

size_t是标准C ++,off_t是Posix,并且off64_t是GNU扩展,具有功能fopen64ftello64等.我认为在64位GNU系统上始终与off_t相同,但是不要在没有检查的情况下将您的公司打赌.

size_t is standard C++, off_t is Posix, and off64_t is a GNU extension that goes with the functions fopen64, ftello64, etc. I think it should always be the same type as off_t on 64 bit GNU systems, but don't bet your company on that without checking.

应该相关,off_t是带符号的,而size_t是未签名的.但是size_t的签名对等物是ptrdiff_t,因此当您需要签名类型时,它并不自动意味着您应该使用off_toff64_t.

Should it be relevant, off_t is signed whereas size_t is unsigned. But the signed counterpart to size_t is ptrdiff_t, so when you need a signed type it doesn't automatically mean you should use off_t or off64_t.

这篇关于size_t和off_t之间的用法区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆