質問

I have a std::vector of some class of the form

class A{
 public:
 A():i(someNumber){}
 void useI() const{
   //do something with i
 }
 // No destructor defined
 private:
  int i;
};

My code is looping through each data point and call useI() on each element in the array. After few hundred iterations, the variable i got corrupted and I have no idea why. The situation can be replicated. I know there is not too much details here but any suggestion on what to check is deeply appreciated.

役に立ちましたか?

解決

The most likely to cause such a thing is overindexing an array access to a pointer that does not point to the correct place. Now, finding where this happens is not trivial, but tools, like valgrind can help.

Working with C-style strings can also easily end up overindexing the string if not properly terminated or the holding buffer's size is applied incorrectly

Another cause could be double-delete/free of a pointer that also corrupts memory

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top