简体   繁体   English

即使分配了 memory,也会出现 Memcpy 分段错误

[英]Memcpy segmentation fault even when memory is allocated

I'm attempting to copy the data from one file by writing it to memory and then copy it into another using memcpy but I'm stumped.我试图通过将数据写入 memory 来从一个文件复制数据,然后使用 memcpy 将其复制到另一个文件,但我很困惑。 I cannot get it to stop giving me segmentation fault.我无法让它停止给我分段错误。 I have a feeling it has something to do with the allocated memory, but I also made sure the file size of the output file is the same as the first one so it wouldn't have that problem and can paste the data into it.我感觉它与分配的 memory 有关,但我还确保 output 文件的文件大小与第一个文件相同,所以它不会有那个问题并且可以将数据粘贴到其中。

edit: I've come to figure it has to do with char out_data and how I'm attempting to copy data into it when it is read only.编辑:我发现它与 char out_data 以及我如何在只读数据时尝试将数据复制到其中有关。 Not sure how to go about this.不知道如何与 go 联系。

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <chrono>
using namespace std;
using namespace std::chrono;



#define OUTPUT_MODE 0700         // protection bits for output file

int main(int argc, char** argv)
{

  auto start = high_resolution_clock::now();
    /* Open the specified file */
    int fd = open(argv[1], O_RDWR);



    // stats.st_size is a variable containing the size of inFile.txt
    struct stat instats;
    if (stat(argv[1], &instats)==0)
            cout<<endl<<"inFile size "<<instats.st_size;
    else
           cout<<"Unable to get file properties.\n";



    /* Get the page size  */
    int pagesize = getpagesize();
    cout<<endl<<"page size is " <<pagesize<<"\n";

    /******************Creation of outFile.txt******************/
    int out_fd = creat(argv[2], OUTPUT_MODE);


    char* in_data = (char*)mmap(NULL, instats.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);

    ftruncate(out_fd, instats.st_size);


    char* out_data = (char*)mmap(NULL, instats.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, out_fd, 0);


// THIS LINE IS THE CULPRIT 
    memcpy(out_data, in_data, instats.st_size);


    /* Unmap the shared memory region */
    munmap(in_data, instats.st_size);
    munmap(out_data, instats.st_size);

    /* Close the file */
    close(fd);
    close(out_fd);

    return 0;
}

creat creates a file which is only open for writing. creat创建一个只为写入而打开的文件。 You cannot mmap such a file descriptor because there is no such thing as write-only memory. If you were to check the return value from the mmap of out_fd , you would find that it failed, with errno == EACCES .你不能mmap这样的文件描述符,因为不存在只写 memory 这样的东西。如果你要检查out_fdmmap的返回值,你会发现它失败了, errno == EACCES When mmap fails it returns an invalid pointer MAP_FAILED (not NULL but typically -1 ) which causes a segfault when memcpy attempts to write there.mmap失败时,它会返回一个无效的指针MAP_FAILED (不是 NULL 但通常是-1 ),当memcpy尝试在那里写入时会导致段错误。

Note that if (!out_data) is therefore the wrong test for failure of mmap ;请注意, if (!out_data)因此是对mmap失败的错误测试; you must use if (out_data == MAP_FAILED) .您必须使用if (out_data == MAP_FAILED)

Instead of creat , use open(argv[2], O_RDWR | O_CREAT | O_TRUNC, OUTPUT_MODE) .而不是creat ,使用open(argv[2], O_RDWR | O_CREAT | O_TRUNC, OUTPUT_MODE)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM