-3

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.

ecain
  • 1,282
  • 5
  • 23
  • 54
musterschüler
  • 171
  • 2
  • 6
  • 3
    You have a bug somewhere in your code. There's only one person on stackoverflow.com who can find and fix this bug. That, of course, would be you, since only you have the complete source code, and only you are capable of running a debugger in order to track down the bug. – Sam Varshavchik Aug 13 '17 at 02:04
  • 1
    Possible duplicate of [Definitive List of Common Reasons for Segmentation Faults](https://stackoverflow.com/questions/33047452/definitive-list-of-common-reasons-for-segmentation-faults) – Weak to Enuma Elish Aug 13 '17 at 02:06
  • Any chance t might be invalid? It is good practice to check the value of t versus the bounds of resp_r – Giel Aug 13 '17 at 02:19
  • Thanks. I am in fact so unfamiliar with c++ that I wasn't sure whether there was a problem with this code itself. Knowing that it somewhere else is already helpful. – musterschüler Aug 13 '17 at 02:20
  • It would be easier to help you is the code would have necessary code to understand the problem (in particular how variables are declared and filled). Have you confirmed that your vectors are properly filled and that the index are valid when you try to retrieve the values. – Phil1970 Aug 13 '17 at 02:36

1 Answers1

0

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.

musterschüler
  • 171
  • 2
  • 6