C++;损坏的双链接列表 我是一个新的C++语言,我来自java,我面临C++的问题。我做了一个小程序来说明我的问题。其主要思想是创建三维数组,并通过读取具有特定文本格式的文本文件来初始化其值。该文件包含作为第一维度的级别数(L)以及作为第二维度和第三维度的行和列大小(N)(数组[L][N][N])

C++;损坏的双链接列表 我是一个新的C++语言,我来自java,我面临C++的问题。我做了一个小程序来说明我的问题。其主要思想是创建三维数组,并通过读取具有特定文本格式的文本文件来初始化其值。该文件包含作为第一维度的级别数(L)以及作为第二维度和第三维度的行和列大小(N)(数组[L][N][N]),c++,C++,编辑:要执行我的程序,我使用g++-std=c++0x program.cpp 节目如下: #include <iostream> #include <string> #include <fstream> using namespace std; int num_of_stages; int N ; string ***stages; void readFile(string file_name){ string line; int po

编辑:要执行我的程序,我使用
g++-std=c++0x program.cpp

节目如下:

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int num_of_stages;
int N ;
string ***stages;

void readFile(string file_name){
    string line;
    int pos;

    ifstream file (file_name);

    if (file.is_open()){

        getline (file,line);                    // L=XXX
        pos = line.find("=");
        line = line.substr(pos+1);
        num_of_stages = atoi(line.c_str());
        cout << "L=" << num_of_stages << endl;

        getline (file,line);                    // N=XXX
        pos = line.find("=");
        line = line.substr(pos+1);
        N = atoi(line.c_str());
        cout << "N=" << N << endl;      

        // setting the stages array with dimensions stages[L][N][N]
        stages = new string**[num_of_stages];
        for(int i = 0 ; i < N ; i ++){
            stages[i] = new string*[N];
            for(int j = 0 ; j < N ; j++){
                stages[i][j] = new string[N];
            }
        }

        for(int i = 0 ; i < num_of_stages ; i++){
            getline (file,line);                // Level comment
            cout << "Level " << line << endl; 
            for(int j = 0 ; j < N ; j++){
                for(int k = 0 ; k < N ; k++){
                    string currentValue ;
                    file >> currentValue;
                    stages[i][j][k] = currentValue;
                    cout << currentValue << " " ;
                }
                cout << endl;
            }
            cout << endl << endl;
            // I am guessing that this getLine() 
            // is necessary to read the end of the line
            // if I try remove it everything goes wrong
            // everytime I print the line's values is ' ' (empty)
            getline (file,line);
            // remove any string value to detect end of file        
            // so the if statement below will be true
            line.clear();
        }
        getline (file,line);                    // END OF MAZE comment
        cout << line << endl;
        if(line != "END OF MAZE"){
            cout << "Wrong file format" << endl;
            file.close();
            exit(0);
        }
        file.close();
    }else{
        cout << "Missing file with name : " << file_name << endl;
        exit(0);
    }
    // program never reach that point
    cout << "Bla bla bla" << endl;
}

int main(){
    readFile("file1.maz");
    return 0;
}
当我使用上面的文件示例执行程序时,我遇到了以下错误:

Error in `./a.out': corrupted double-linked list: 0x0000000001174280 ***
以下是gdb回溯:

(gdb) backtrace
#0  0x00007ffff7531cc9 in __GI_raise (sig=sig@entry=6)
    at ../nptl/sysdeps/unix/sysv/linux/raise.c:56
#1  0x00007ffff75350d8 in __GI_abort () at abort.c:89
#2  0x00007ffff756e394 in __libc_message (do_abort=do_abort@entry=1, 
    fmt=fmt@entry=0x7ffff767cb28 "*** Error in `%s': %s: 0x%s ***\n")
    at ../sysdeps/posix/libc_fatal.c:175
#3  0x00007ffff75790f7 in malloc_printerr (action=<optimized out>, 
    str=0x7ffff7678bfc "corrupted double-linked list", ptr=<optimized out>)
    at malloc.c:4996
#4  0x00007ffff757a7a7 in _int_free (av=0x7ffff78b9760 <main_arena>, p=<optimized out>, 
    have_lock=0) at malloc.c:3996
#5  0x00007ffff7b898be in std::basic_filebuf<char, std::char_traits<char> >::_M_destroy_internal_buffer() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#6  0x00007ffff7b89d47 in std::basic_filebuf<char, std::char_traits<char> >::close() ()
   from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#7  0x00007ffff7b8b3ad in std::basic_ifstream<char, std::char_traits<char> >::close() ()
   from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#8  0x00000000004018b9 in readFile(std::string) ()
#9  0x0000000000401a3d in main ()
(gdb)回溯
#0 0x00007FF7531CC9英寸GI上升(信号=sig@entry=6)
位于../nptl/sysdeps/unix/sysv/linux/raise.c:56
#1 0x00007FF75350D8 in_uuugi_abort()位于abort.c:89
#2 0x00007FF756E394在\uuuu libc\u消息中(do\u abort=do_abort@entry=1, 
fmt=fmt@entry=0x7ffff767cb28“`%s'中的***错误:%s:0x%s***\n”)
地址../sysdeps/posix/libc_fatal.c:175
#malloc_printerr中的3 0x00007ffff75790f7(操作=,,
str=0x7ffff7678bfc“损坏的双链接列表”,ptr=)
在malloc.c:4996
#4 0x00007FF757A7A7A7在无内部(av=0x7ffff78b9760,p=,
在malloc处有_lock=0)。c:3996
#std::basic_filebuf::\u M_destroy_internal_buffer()()中的5 0x00007ffff7b898be来自/usr/lib/x86_64-linux-gnu/libstdc++.so.6
#std::basic_filebuf::close()中的6 0x00007FF7B89D47
来自/usr/lib/x86_64-linux-gnu/libstdc++.so.6
#std::basic_ifstream::close()中的7 0x00007FF7Bb8B3AD
来自/usr/lib/x86_64-linux-gnu/libstdc++.so.6
#读取文件(std::string)()中的8 0x00000000004018b9
#9 0x0000000000401a3d主目录()
我看过其他关于当前问题的帖子,其中一些帖子是关于获取数组索引的,但我已经测试了所有内容,我总是在范围内。那么我读取文件的方式是否有问题?。 我希望这不是一个愚蠢的问题。
提前谢谢。

您已经越界了:

stages = new string**[num_of_stages];
    for(int i = 0 ; i < N ; i ++) {
        stages[i] = new string*[N];
stages=新字符串**[num_of_stages];
对于(int i=0;i
如果
num\u of\u stages
您正在通过数组进行访问


您应该使用
std::vector
,而不是处理原始指针和自己使用
new/delete
。它有一个
at
函数,可以进行边界检查。

您正在越界:

stages = new string**[num_of_stages];
    for(int i = 0 ; i < N ; i ++) {
        stages[i] = new string*[N];
stages=新字符串**[num_of_stages];
对于(int i=0;i
如果
num\u of\u stages
您正在通过数组进行访问


你应该使用
std::vector
,而不是处理原始指针和自己使用
new/delete
。它有一个执行边界检查的
at
函数。

你应该避免感谢这个建议你应该避免感谢这个建议我更多地关注的是文件读取,而不是数组初始化..--谢谢你的帮助和建议..我更多地关注的是文件读取,而不是数组初始化..--谢谢你的帮助和建议。。