[英]stat64 returning 32-bit st_size on 64bit ubuntu
unsigned char *map_file(char *filename, uint64_t *len) {
uint64_t fd = open64(filename, O_RDONLY);
struct stat64 st;
fstat64(fd, &st)
unsigned char *map;
map = (unsigned char *)mmap64(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
st.st_size ends up being 4294967295 for large files (I'm testing against 8.7gb file) and is causing segmentation faults (at 47%). 对于大文件,st.st_size最终为4294967295(我正在测试8.7gb文件)并导致分段错误(47%)。 Machine is 64-bit and OS (ubuntu) is 64 bit. 机器是64位,OS(ubuntu)是64位。 What am I doing incorrectly? 我做错了什么?
You probably need to define one of these macros. 您可能需要定义其中一个宏。
http://www.kernel.org/doc/man-pages/online/pages/man7/feature_test_macros.7.html http://www.kernel.org/doc/man-pages/online/pages/man7/feature_test_macros.7.html
_LARGEFILE64_SOURCE
Expose definitions for the alternative API specified by the LFS (Large
File Summit) as a "transitional extension" to the Single UNIX
Specification. (See http://opengroup.org/platform/lfs.html.) The
alternative API consists of a set of new objects (i.e., functions and
types) whose names are suffixed with "64" (e.g., off64_t versus off_t,
lseek64() versus lseek(), etc.). New programs should not employ this
interface; instead _FILE_OFFSET_BITS=64 should be employed.
_FILE_OFFSET_BITS
Defining this macro with the value 64 automatically converts references
to 32-bit functions and data types related to file I/O and file system
operations into references to their 64-bit counterparts. This is
useful for performing I/O on large files (> 2 Gigabytes) on 32-bit
systems. (Defining this macro permits correctly written programs to
use large files with only a recompilation being required.) 64-bit
systems naturally permit file sizes greater than 2 Gigabytes, and on
those systems this macro has no effect.
尝试将-D_FILE_OFFSET_BITS=64
添加到编译行。
You should not be using the *64()
versions of any of these functions. 您不应该使用任何这些函数的*64()
版本。 On 64 bit Linux, off_t
is 64 bit, and always has been. 在64位Linux上, off_t
是64位,并且一直都是。 Simply use: 只需使用:
int fd = open(filename, O_RDONLY);
struct stat st;
unsigned char *map;
fstat(fd, &st);
map = mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
(with error-checking, of course). (当然有错误检查)。 I have tested the above with a > 8GB file, and it works fine. 我用> 8GB文件测试了上面的内容,它运行正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.