0

我正在从文件中提取数据(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)

如果我关闭它,它会在那时出现故障。

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

4

3 回答 3

0

您似乎通过在缓冲区外写入​​来破坏内存。它不在您显示的代码中。你一定遗漏了什么。

于 2013-08-22T10:54:50.080 回答
0

我不是假装这是任何形式的答案。我只是发布我自己的结果并要求您对照自己的结果进行检查....

我假设您还有其他未显示的代码。我想知道您的其他代码是否是问题的根源;也许它正在破坏内存中的其他东西,也许是堆栈?无论如何,这是我的整个测试(减去必要的#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";
}
于 2013-08-22T07:03:20.533 回答
0
while (getline(theta_extracter, line)){
.....
 while (stream >> exported){
            thetas.push_back(atof(exported.c_str())); /* Store value*/
        }
}

我觉得你应该这样做!!

于 2013-08-22T06:27:47.250 回答