简体   繁体   English

当mmap()使用JPG文件时,为什么会出现内存错误?

[英]Why do I get memory errors when mmap()'ing a JPG file?

I have the following section of code: 我有以下代码部分:

char*
Sender::PrepareData(char* filename, unsigned long long int bytesToTransfer)
{
    int fd, pagesize;
    char *data;

    ifstream file(filename, ios::binary | ios::ate);
    int size = file.tellg();
    cout << "File Size: " << size << endl;

    if(size < bytesToTransfer)
        {cout << "File smaller than specified number of bytes {" << bytesToTransfer << "} to transfer -- Exiting!\n"; exit(1);}

    fd = open(filename, O_RDONLY);
    if (fd==NULL) {fputs ("File error",stderr); exit (1);}

    cout << "File Open: " << filename << endl;

    pagesize = getpagesize();
    cout << "Pagesize: " << pagesize << endl;

    data = static_cast<char*>(mmap((caddr_t)0, bytesToTransfer, PROT_READ, MAP_SHARED, fd, 0));
    if (*data == -1) {fputs ("Memory error",stderr); exit (2);}

    return data;
}

This seemingly works fine for text and .deb files - however, when trying with a ~3MB image file (.jpg), I get memory errors: 这似乎适用于文本和.deb文件 - 但是,当尝试使用~3MB图像文件(.jpg)时,我会收到内存错误:

File Size: 3333840
File Open: t1.jpg
Pagesize: 4096
Memory error[Inferior 1 (process 3293) exited with code 02]

Am I using mmap() wrong? 我使用mmap()错了吗? I'm trying to write a simple wrapper that will take any type of file and return a char* containing the specified number of bytes. 我正在尝试编写一个简单的包装器,它将获取任何类型的文件并返回包含指定字节数的char *。

You should be checking just data == -1 (or, even better, MAP_FAILED ) and not *data == -1 (dereferencing via * is wrong here). 您应该只检查data == -1 (或者更好, MAP_FAILED )而不是 *data == -1 (通过*解除引用是错误的)。

The reason your code fails is because the first byte of every JPEG file is FF in hex or -1 in signed decimal. 您的代码失败的原因是因为每个JPEG文件的第一个字节是十六进制的FF或带符号十进制的-1。

For more detail on JPEG, Google for "JPEG file format." 有关JPEG的更多详细信息,请参阅Google的“JPEG文件格式”。 For example, search for "SOI" on this page . 例如,在此页面上搜索“SOI”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ftp&#39;ing后如何获取文件的大小? - How do I get the size of a file after ftp'ing? 尝试在C ++中包含头文件时为什么会出现错误? - Why do I get errors when I try to include a header file in C++? C ++ M将文件映射到内存,然后将文件描述符获取到该内存 - C++ MMap a file to memory and then get a file descriptor to that memory 我是否需要munmap()mmap()文件? - Do I have to munmap() a mmap() file? 如何映射内存中的_particular_区域? - How do I mmap a _particular_ region in memory? 当我链接到库时,为什么会出现错误,而不是在构建它时出错? - Why do I get errors when I link to a library, but not when I build it? 当从c#访问内存映射文件而不是从c ++访问内存映射文件时,为什么会出现访问被拒绝异常 - why do I get an access denied exception when accessing a memory mapped file from c# but not from c++ 为什么会出现这些链接错误? - Why do I get these link errors? 为什么在OpenCV中访问此矩阵时会出现内存错误? - Why am I getting memory errors when accessing this matrix in OpenCV? 为什么我初始化数组容器的元素大于数组大小时没有出错? - Why I do not get errors when I initialize an array container with more elements than the size of the array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM