C ++ - 关闭ifstream时的分段错误

时间:2013-08-22 06:18:56

标签: c++ segmentation-fault ifstream

我正在使用theta.txt')从文件ifstream中提取数据。

std::ifstream theta_extracter("theta.txt", std::ifstream::in);  
std::string line;
std::vector <double> thetas;

    /* For each line in file until EOF */
while (true){
    getline(theta_extracter, line); /* Get line */

    if (theta_extracter.eof()) break; /* Stop at EOF*/

    std::stringstream stream(line); /* Parse Line */
    std::string exported;

    /*Parse line*/
    while (true){
        stream >> exported;
        thetas.push_back(atof(exported.c_str())); /* Store value*/
        if (stream.eof()) break;    
    }
}

theta_extracter.close();

如果我不关闭theta_extracter,我会收到一条错误消息:

    *** glibc detected *** ./combi: free(): invalid next size (fast): 0x0000000000b69ba0 ***
======= Backtrace: =========
/lib64/libc.so.6[0x36c98760e6]
/lib64/libc.so.6[0x36c9878c13]
./combi[0x405964]
./combi[0x4042ca]
./combi[0x402e95]
./combi[0x40266f]
./combi[0x401e19]
./combi[0x401471]
/lib64/libc.so.6(__libc_start_main+0xfd)[0x36c981ecdd]
./combi[0x4012c9]
======= Memory map: ========
00400000-0040d000 r-xp 00000000 00:2a 59351995                           /nfs/stak/students/m/moissinb/WORKSPACE/EXP3/VCG Solver/combi
0060c000-0060d000 rw-p 0000c000 00:2a 59351995                           /nfs/stak/students/m/moissinb/WORKSPACE/EXP3/VCG Solver/combi
00b5f000-00c4a000 rw-p 00000000 00:00 0                                  [heap]
36c9400000-36c9420000 r-xp 00000000 fd:00 2613                           /lib64/ld-2.12.so
36c961f000-36c9620000 r--p 0001f000 fd:00 2613                           /lib64/ld-2.12.so
36c9620000-36c9621000 rw-p 00020000 fd:00 2613                           /lib64/ld-2.12.so
36c9621000-36c9622000 rw-p 00000000 00:00 0 
36c9800000-36c998a000 r-xp 00000000 fd:00 5271                           /lib64/libc-2.12.so
36c998a000-36c9b89000 ---p 0018a000 fd:00 5271                           /lib64/libc-2.12.so
36c9b89000-36c9b8d000 r--p 00189000 fd:00 5271                           /lib64/libc-2.12.so
36c9b8d000-36c9b8e000 rw-p 0018d000 fd:00 5271                           /lib64/libc-2.12.so
36c9b8e000-36c9b93000 rw-p 00000000 00:00 0 
36c9c00000-36c9c83000 r-xp 00000000 fd:00 5272                           /lib64/libm-2.12.so
36c9c83000-36c9e82000 ---p 00083000 fd:00 5272                           /lib64/libm-2.12.so
36c9e82000-36c9e83000 r--p 00082000 fd:00 5272                           /lib64/libm-2.12.so
36c9e83000-36c9e84000 rw-p 00083000 fd:00 5272                           /lib64/libm-2.12.so
36cfc00000-36cfc16000 r-xp 00000000 fd:00 5275                           /lib64/libgcc_s-4.4.7-20120601.so.1
36cfc16000-36cfe15000 ---p 00016000 fd:00 5275                           /lib64/libgcc_s-4.4.7-20120601.so.1
36cfe15000-36cfe16000 rw-p 00015000 fd:00 5275                           /lib64/libgcc_s-4.4.7-20120601.so.1
36d0400000-36d04e8000 r-xp 00000000 fd:03 548265                         /usr/lib64/libstdc++.so.6.0.13
36d04e8000-36d06e8000 ---p 000e8000 fd:03 548265                         /usr/lib64/libstdc++.so.6.0.13
36d06e8000-36d06ef000 r--p 000e8000 fd:03 548265                         /usr/lib64/libstdc++.so.6.0.13
36d06ef000-36d06f1000 rw-p 000ef000 fd:03 548265                         /usr/lib64/libstdc++.so.6.0.13
36d06f1000-36d0706000 rw-p 00000000 00:00 0 
7fc067c6a000-7fc067c6f000 rw-p 00000000 00:00 0 
7fc067c96000-7fc067c99000 rw-p 00000000 00:00 0 
7fff52f65000-7fff52f7a000 rw-p 00000000 00:00 0                          [stack]
7fff52fff000-7fff53000000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
Abort (core dumped)

如果我关闭它,它会在那时发生错误。

关于出了什么问题的任何想法?

3 个答案:

答案 0 :(得分:0)

while (getline(theta_extracter, line)){
.....
 while (stream >> exported){
            thetas.push_back(atof(exported.c_str())); /* Store value*/
        }
}

我认为你应该这样做!!

答案 1 :(得分:0)

我不是假装这是任何答案。我只是发布自己的结果&amp;要求你自己检查一下......

我假设你有其他代码没有显示。我想知道你的其他代码是否是问题的根源;也许它会腐蚀内存中的其他东西,也许是堆栈?无论如何,这是我的整个测试(减去必要的#includes),它运行时没有崩溃我的Linux x86盒子。这段代码在你的系统上做了什么?

int main() {

    std::cout << "Beginning..." << std::endl;
    std::ifstream theta_extracter("theta.txt", std::ifstream::in);
    std::string line;
    std::vector <double> thetas;

    /* For each line in file until EOF */
    while (true){
        getline(theta_extracter, line); /* Get line */
    std::cout << "\nLine: \"" << line << "\"" << std::endl;

        if (theta_extracter.eof()) break; /* Stop at EOF*/

        std::stringstream stream(line); /* Parse Line */
        std::string exported;

        /*Parse line*/
        while (true){
            stream >> exported;
            std::cout << "exported = \"" << exported << "\"" << std::endl;
            thetas.push_back(atof(exported.c_str())); /* Store value*/
            if (stream.eof()) break;
        }
    }

// I get the same results whether I close theta_extractor or not....
//    theta_extracter.close();

    std::vector<double>::iterator thit = thetas.begin();
    while (thit != thetas.end()) {
        std::cout << ' ' << *thit;
        ++thit;
    }
    std::cout << "\n\n...done.\n";
}

答案 2 :(得分:0)

您似乎通过编写外部缓冲区来破坏内存。它不在您显示的代码中。你必须省略一些东西。

相关问题