2013-11-24 33 views
1

我有问题Valgrind的无效读取,Valgrind的:尺寸8

我创建了这个类:

class shop { 

    private: 
     vector<vector<string> > products_; 

    public: 
      shop(string ProductFile); 
      FindMinPrice(string product); 
} 

    //method in the cpp file 
    vector<string> shop::FindMinPrice(string product){ 
     string ProductName=(string)products_[0][0]; 

    } 

我没有写整个代码,但它与GCC编译器做工精细。 当我运行valgrind检查它显示: 无效的读取大小为8 和在日食它发送给我的ProductName行。

设计有什么问题?以及如何让GCC编译并运行,但VALGRIND崩溃?

谢谢。

回答

3

看起来您的products_载体向量是空的,这意味着元素products_[0][0]的访问是未定义的行为。

关于未定义行为的不幸之处在于,您的程序看起来可能正常工作,甚至可能完成而没有任何可见问题。

0
  1. 在C++中使用c-style转换不是一个好习惯。
  2. 您的商店方法不会返回任何值。