musterschüler
musterschüler

Reputation: 181

invalid pointer when passing string in c++

I have two c++ classes, let's call one hier and the other one cp. During startup, hier creates an instance of cp. cp reads in a text file with two columns, which are called resp_r and resp_l, and stores the contents in two arrays of the same name. For example: std::vector<int> resp_r;

For the curious reader, these columns represent whether a left or right key press has occurred within a 1 second window.

In each cell, resp_r/l then either has a 1 or 0, depending on the text file.

hier knows about the number of lines (t) in the text file, and has a for loop over t. In each loop, it "asks" cp what the content is in resp_r/l[t], by calling the public method of cp: string get_response(int t):

string resp;
while (t < max_t) {
 resp = cp->get_response(t);
 ...
}

in cp, the get_response function is defined like so:

string CP::get_response(int t) {                                                                                                                                                                                                
  if (resp_r[t] == 1) {                                                                                                                                                                                                                       
    return "right";                                                                                                                                                                                                                           
  } else if (resp_l[t] == 1) {                                                                                                                                                                                                                
    return "left";                                                                                                                                                                                                                            
  }                                                                                                                                                                                                                                           
  return "none";                                                                                                                                                                                                                              
}  

I have about 50 files for which this works fine, but in one of them it doesn't when the script gets close to the end of the file.

* Error in `./cp_diff_vr_x.out': free(): invalid pointer: 0x0000000000f3e920 *

I have tried to figure out what is wrong here. The error seems to occur during the return statement (return "right", to be specific), rather than during querying resp_r.

Upvotes: -3

Views: 257

Answers (1)

mustersch&#252;ler
mustersch&#252;ler

Reputation: 181

OK, OK, Sam Varshavchik was right, the bug was in a completely unrelated place, accessing an array in another function (inside of cp) beyond its allocated size. Shocking!

I found the bug with valgrind, which gave me an invalid read size error.

Wow. It's been too long since I wrote c++ code. Time to wrap up this project and send it AWAY!

Thanks everybody.

Upvotes: 0

Related Questions