c ++是32位编译时的glibc无效指针错误

时间:2013-07-15 22:09:12

标签: c++ memory 32bit-64bit ifstream sstream

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

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

使用gdb进行调试显示文件打开,成功关闭,所有数据都成功存储,然后退出input.txt方法时最后抛出SIGABRT。

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

ReadData()

输入文本文件:

#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 ()

#11 #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 test_3.cpp:91方法的结束括号。

2 个答案:

答案 0 :(得分:0)

首先请注意,您没有包含要测试的示例input.txt。第二,注意,变量初始化为什么样本值?

所以,鉴于tf=0.0t0=0.0delt=1.0并使用input.txt

>
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的格式符合代码要求吗?你真的想删除列表中的最后一项吗?

答案 1 :(得分:0)

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