0

我编写了一个程序,可以在我的 64 位机器(运行 linux SUSE)上编译并运行良好。现在我需要调用一个外部库,但我只能访问 32 位二进制文​​件。我的源代码编译并从 ssh 命令行无错误地链接到 32 位机器,但我现在在调用库之前在运行时遇到内存错误,或者发生任何有趣的事情......

我有一个简单的类cWorld来初始化其他一些类,它有一个方法cWorld::ReadData()可以打开一个文本文件并从文件中解析/读取行并将值存储在 的各个成员中cWorld,然后关闭文件。文件input.txt仅包含一些解释文本和初始条件值,用逗号和分号分隔。没有什么开创性的!

用gdb调试显示文件打开,关闭成功,所有数据都存储成功,然后ReadData()退出方法最后抛出SIGABRT。

从我的程序中提取问题代码:

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>


class cWorld {
    public:
        cWorld ();
        void CallReadData ();
    private:
        int N_target, N_steps;
        double t0, tf, delt;
        std::vector<double> data;
        void ReadData ();
};


cWorld::cWorld () {
    N_target = 0;
    N_steps = 0;
    delt = 0.0;
    t0 = 0.0;
    tf = 0.0;
}

void cWorld::CallReadData() {
    ReadData();
}


void cWorld::ReadData() {
    std::string line;
    std::ifstream input("input_test.txt");

    if (input.is_open()) {

         // RETRIEVE INPUT OPTIMIZATION PARAMETERS

        input.ignore(1000, '>');              // ignore text until first '>' appears
        std::getline(input, line, ';');       // get int N_target
        std::stringstream(line) >> N_target;

        input.ignore(1000, '>');              // ignore text until first '>' appears
        std::getline(input, line, ',');       // get t0
        std::stringstream(line) >> t0;

        std::getline(input, line, ',');       // get delt
        std::stringstream(line) >> delt;
        std::cout << "delt = " << delt << std::endl;

        std::getline(input, line, ',');       // get tf
        std::stringstream(line) >> tf;

        N_steps = (int)( (tf - t0) / delt ) + 1;   // set an int cWorld::N_steps

        // RETRIEVE INPUT STATE PARAMETERS

        int index = 0;                       // initialize local iterator
        data.resize(12*N_target, 0.0);       // set data size
        std::cout << "data elements = " << data.size() << std::endl;

        while (!input.eof()) {

            // if there's '<' end loop
            if (input.peek() == '<') break;

            // if there's a semicolon, store following text in data...
            else if (input.peek() == ';') {
                input.ignore(1000, '>');
                std::getline(input, line, ',');
                std::stringstream(line) >> data[index];
                index++;
            }

            // else if there's a comma, store following text in data...
            else {
                std::getline(input, line, ',');
                std::stringstream(line) >> data[index];
                index++;
            }

        }

        input.close();

    }

    else  std::cout << "Can't open file 'input.txt'.\n";
}




int main() {

    cWorld world_1;
    world_1.CallReadData();

    return 0;

}

输入文本文件:

/****************************************************************/
/*                                                              */
/*  p2pOpt.C INPUT FILE                                         */
/*                                                              */
/****************************************************************/

System Parameters: number of paths to optimize
format: N_target; (int)


>3;


System Parameters: start time, step size, end time
format: t0,delt,tf,; (doubles)


>0.0,0.001,1,;


Target 1 Parameters: Initial Conditions
format: x,y,z,theta1,theta2,theta3,xdot,ydot,zdot,theta1dot,theta2dot,theta3dot,;(doubles)

>1.0,0.0,0.0,3.14159265359,0.0,0.0,1.0,0.0,0.0,1.0,0.0,0.0,;
>2.0,2.0,2.0,2.0,2.0,2.0,2.0,2.0,2.0,2.0,2.0,2.0,;
>3.0,3.0,3.0,3.0,3.0,3.0,3.0,3.0,3.0,3.0,3.0,3.0,;
<

这是调试输出:

======= Memory map: ========
08048000-0804b000 r-xp 00000000 00:29 18254842   /home/ston_sa/core/motion_planning/algorithms_cpp/p2pOpt/test_3_32
0804b000-0804c000 r--p 00002000 00:29 18254842   /home/ston_sa/core/motion_planning/algorithms_cpp/p2pOpt/test_3_32
0804c000-0804d000 rw-p 00003000 00:29 18254842   /home/ston_sa/core/motion_planning    /algorithms_cpp/p2pOpt/test_3_32
0804d000-0806e000 rw-p 00000000 00:00 0          [heap]
b7b00000-b7b21000 rw-p 00000000 00:00 0
b7b21000-b7c00000 ---p 00000000 00:00 0
b7cd8000-b7cdb000 rw-p 00000000 00:00 0
b7cdb000-b7e42000 r-xp 00000000 08:06 114523898  /lib/libc-2.11.3.so
b7e42000-b7e44000 r--p 00167000 08:06 114523898  /lib/libc-2.11.3.so
b7e44000-b7e45000 rw-p 00169000 08:06 114523898  /lib/libc-2.11.3.so
b7e45000-b7e48000 rw-p 00000000 00:00 0
b7e48000-b7e64000 r-xp 00000000 08:06 114544736  /lib/libgcc_s.so.1
b7e64000-b7e65000 r--p 0001b000 08:06 114544736  /lib/libgcc_s.so.1
b7e65000-b7e66000 rw-p 0001c000 08:06 114544736  /lib/libgcc_s.so.1
b7e66000-b7e8c000 r-xp 00000000 08:06 114353773  /lib/libm-2.11.3.so
b7e8c000-b7e8d000 r--p 00026000 08:06 114353773  /lib/libm-2.11.3.so
b7e8d000-b7e8e000 rw-p 00027000 08:06 114353773  /lib/libm-2.11.3.so
b7e8e000-b7f70000 r-xp 00000000 08:06 2169219    /usr/lib/libstdc++.so.6.0.16
b7f70000-b7f74000 r--p 000e2000 08:06 2169219    /usr/lib/libstdc++.so.6.0.16
b7f74000-b7f75000 rw-p 000e6000 08:06 2169219    /usr/lib/libstdc++.so.6.0.16
b7f75000-b7f7c000 rw-p 00000000 00:00 0
b7fdd000-b7fdf000 rw-p 00000000 00:00 0
b7fdf000-b7ffe000 r-xp 00000000 08:06 114544574  /lib/ld-2.11.3.so
b7ffe000-b7fff000 r--p 0001e000 08:06 114544574  /lib/ld-2.11.3.so
b7fff000-b8000000 rw-p 0001f000 08:06 114544574  /lib/ld-2.11.3.so
bffdf000-c0000000 rw-p 00000000 00:00 0          [stack]
ffffe000-fffff000 r-xp 00000000 00:00 0          [vdso]

Program received signal SIGABRT, Aborted.
0xffffe424 in __kernel_vsyscall ()

和回溯:

#0  0xffffe424 in __kernel_vsyscall ()
#1  0xb7d05e20 in raise () from /lib/libc.so.6
#2  0xb7d07755 in abort () from /lib/libc.so.6
#3  0xb7d44d65 in __libc_message () from /lib/libc.so.6
#4  0xb7d4ac54 in malloc_printerr () from /lib/libc.so.6
#5  0xb7d4c563 in _int_free () from /lib/libc.so.6
#6  0xb7d4f69d in free () from /lib/libc.so.6
#7  0xb7f3fa0f in operator delete(void*) () from /usr/lib/libstdc++.so.6
#8  0xb7f26f6b in std::string::_Rep::_M_destroy(std::allocator<char> const&) () from /usr/lib/libstdc++.so.6
#9  0xb7f26fac in ?? () from /usr/lib/libstdc++.so.6
#10 0xb7f2701e in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string() () from /usr/lib/libstdc++.so.6
#11 0x080495bf in cWorld::ReadData (this=0xbfffefe0) at test_3.cpp:91
#12 0x0804961b in cWorld::CallReadData (this=0xbfffefe0) at test_3.cpp:30
#13 0x08049646 in main () at test_3.cpp:100

#11test_3.cpp:91ReadData()方法的右括号。

4

2 回答 2

0

您的第一个问题是您的循环进行了 37 次读取,但仅将大小调整data为 36 个元素。您应该重新构建解析输入的方式。如果没有别的,也许可以使用 scanf() 。

于 2013-07-16T20:57:08.177 回答
0

首先请注意,您没有包含input.txt要测试的样本。第二个注意事项,变量初始化为哪些样本值?

因此,鉴于tf=0.0,t0=0.0delt=1.0and 使用input.txtof:

>
1;
1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,12.0
<

我得到一个包含 11 个条目的数据向量,列表中的前 11 个值没有错误。您确定您input.txt的格式符合代码预期吗?您真的要删除列表中的最后一项吗?

于 2013-07-15T23:14:41.830 回答