簡體   English   中英

將數組傳遞給C ++函數時出現段錯誤

[英]Segfault when passing array to C++ function

我正在編寫一個函數來從C ++中的二進制文件讀取數組。 這是我的功能:

#include <iostream>
#include <fstream>

template <typename T>
int bread(T array_out[], int array_size, const char FILENAME[]) {
    // Open file
    std::ifstream input_file (FILENAME, std::ios::in | std::ios::binary
            | std::ios::ate);
    // Check that the file was opened correctly
    if (input_file.is_open()) {
        // Check that the file size matches the array size
        std::streampos file_size = input_file.tellg();
        if (file_size == array_size) {
            // Read the array contents from the file
            input_file.seekg(0, std::ios::beg);
            input_file.read(reinterpret_cast<char *>(&array_out), array_size);
            // Close file
            input_file.close();
        } else {
            std::cerr << "File " << FILENAME << " is not of expected size.\n";
            std::cerr << "  File size:\t" << file_size << " B\n";
            std::cerr << "  Expected\t" << array_size << " B\n";
            // Close file
            input_file.close();
            return -2;
        }
    } else {
        std::cerr << "Failed to open file " << FILENAME << "\n";
        return -1;
    }

    return 0;
}

沒有| std::ios::ate | std::ios::ate ,這將計算文件大小0並返回-2 那講得通。

但是,一旦我包括| std::ios::ate | std::ios::ate (或者,或者, input_file.seekg(0, std::ios::end); ),該函數的執行失敗,並出現段錯誤。 由於input_file.seekg(0, std::ios::beg);在某些情況下,在文件內進行清晰查找是input_file.seekg(0, std::ios::beg); 可以正常工作。

我的猜測是輸入文件中可能沒有EOF標記。 如果是這樣,是否有某種方法可以安全地進行處理?

編輯:該函數在這里調用:

int verify_buff[SIZE];
Ierr = bread(verify_buff, sizeof(verify_buff), "serial.bin");
if (Ierr != 0) {
    std::cerr << "Error " << Ierr << " in function bread\n";
    return Ierr;
}

其中SIZE是定義為1024的全局常量。

在segfault,

array_size = 4096
array_out = 0x7ffd5dca20e0

完整的段錯誤消息:

[physlogin:81411] *** Process received signal ***
[physlogin:81411] Signal: Segmentation fault (11)
[physlogin:81411] Signal code: Address not mapped (1)
[physlogin:81411] Failing at address: 0x1
[physlogin:81411] [ 0] /lib64/libpthread.so.0[0x3cb300f7e0]
[physlogin:81411] [ 1] /lib64/libc.so.6(fclose+0x4)[0x3cb2466344]
[physlogin:81411] [ 2] /usr/lib64/libstdc++.so.6(_ZNSt12__basic_fileIcE5closeEv+0x4c)[0x3cb78ba5ac]
[physlogin:81411] [ 3] /usr/lib64/libstdc++.so.6(_ZNSt13basic_filebufIcSt11char_traitsIcEE5closeEv+0xb7)[0x3cb786cb77]
[physlogin:81411] [ 4] /usr/lib64/libstdc++.so.6(_ZNSt14basic_ifstreamIcSt11char_traitsIcEE5closeEv+0xd)[0x3cb786ebfd]
[physlogin:81411] [ 5] Q3_gw639.exe[0x40dec5]
[physlogin:81411] [ 6] Q3_gw639.exe[0x40ea06]
[physlogin:81411] *** End of error message ***

一個主要問題是您致電此處read

 input_file.read(reinterpret_cast<char *>(&array_out), array_size);

由於array_out已經是T* ,所以此行嘗試讀取指針的地址。 這是不正確的-您要執行的操作是:

 input_file.read(reinterpret_cast<char *>(array_out), array_size);

請注意,您的函數聲明:

template <typename T>
int bread(T array_out[], int array_size, const char FILENAME[]) 

與此沒有什么不同:

template <typename T>
int bread(T* array_out, int array_size, const char* FILENAME) 

即使第一個看起來像正在接收一個數組,但事實並非如此。 通過名稱傳遞數組會衰減傳遞給指針的參數,因此您正在處理函數中的指針。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM