2
int fromVectoEigen(vector<string> & source, double ** & array, int & n)
{  
    cout<<"n:"<<n<<'\n';
    int counter = 0;

    for (int j = n-1 ; j >= 0 ; j--) // iterate through each line
    {
        string mystring = source.back(); // take last line

        counter++;
        char * str = new char [mystring.length()+1];
        std::strcpy (str, mystring.c_str()); // copy string to char array
        char * pch; // pointer to tokens
        pch = strtok (str," ,-");
        for( int i = 0 ; i<3 ; i++) // dismiss first 3 columns
        {

            pch = strtok (NULL, " ,-");
        }

        for (int i= 0 ; i<n ; i++)
        {
            double val = atof(pch); // cast char to double

            //array[j][i]= val;
            //cout<<array[j][i]<<'\t';
            //pch = strtok (NULL, " ,-");
        }
        //
        //source.pop_back();

        }

    return 0;
}

嘿!

使用此功能,我想从文件中读取矩阵到二维数组该矩阵的行数与列数一样多。我想用strtok按空格分割行。这些行已经被读入另一个函数的向量中。(有效 - 测试了它)。所以我不知道这个问题,因为我试图用一个小矩阵 4 列 4 行来运行它,效果很好!现在我想用一个有 1000 多行和尽可能多的列的大矩阵来尝试它。我从 valgrind 收到此错误消息:

大小 1 ==26501== 在 0x58A87AB 处的无效读取:_ _strtod_l_internal (strtod_l.c:538) ==26501== 0x4015BB:fromVectoEigen(std::vector >&, double**&, int&) (topo.cpp :70) ==26501== by 0x40362B: main (main.cpp:36) ==26501== 地址 0x0 没有被堆栈、malloc 或(最近)释放

我尝试了注释/取消注释方法,在我使用 atof 的地方一切正常……我不明白,因为它与小矩阵一起工作得很好

大矩阵中的值如下所示: 0.11991517 在小测试矩阵中,我只有 0 或 0.1 之类的值

我希望我解释得足够多......如果需要,请询问更多细节。

4

2 回答 2

3

你 allocate str,但你永远不会释放它......并且使用你拥有的代码,你永远不能。因此,每次调用此函数时,都会泄漏内存。

您应该在delete [] str;完成后添加 a ,或者根本不使用动态内存并坚持使用std::string.

此外,您可以避免整个转换问题,只需使用std::istream::operator>>(例如 using std::istringstream)来解析您的输入。

像这样的东西(未经测试)应该更适合你:

struct reader : std::ctype<char>
{
    reader() : std::ctype<char>(get_table()) {}

    static std::ctype_base::mask const* get_table()
    {
        static std::vector<std::ctype_base::mask> rc(table_size, std::ctype_base::mask());
        rc['\n'] = std::ctype_base::space;
        rc[','] = std::ctype_base::space;
        rc['-'] = std::ctype_base::space;
        rc[' '] = std::ctype_base::space;
        rc['\t'] = std::ctype_base::space;
        return &rc[0];
    }
};

int fromVectorEigen(const std::vector<std::string>& source, std::vector<std::vector<double>>& destination)
{
    reader rdr;
    std::for_each(source.rbegin(), source.rend(), [&](const std::string& s)
    {
        std::istringstream iss(s);
        iss.imbue(std::locale(), &rdr);
        std::string ignored_columns;
        double value;
        iss >> ignored_columns >> ignored_columns >> ignored_columns;
        std::vector<double> values;
        while (iss >> value)
        {
            values.push_back(value);
        }
        destination.push_front(values);
    });
    return 0;
}
于 2014-02-13T21:43:08.720 回答
0

如果您想保持当前不使用流的设计,但消除显式分配内存,您可以使用 std::vector:

  #include <vector>
  //...
  std::vector<char> str(mystring.begin(), mystring,end());
  str.push_back(0);
  char * pch; // pointer to tokens
  pch = strtok (&str[0]," ,-");
于 2014-02-13T22:19:45.470 回答