[Solved] Segmentation Fault C++ pointers [closed]


Your code is no C++ code. Except file operations it is (bad styled) C code. You are using a plain array (cells), you even do an unnecessary copies of the array (root) and that pointer arithemtic is dangerous (as you are currently experiencing).

I think you should rewrite your code a bit which will solve your problem:

  1. You should use descriptive variable names… k,s,i,etc. are hard to read
  2. Use a two-dimensional array for ‘cells’. Or even better a C++ container like a vector of vectors. The latter would check boundaries and you could get rid of your pointer arithmetics (which causes such faults when done the wrong way) and you could use plain indexes.
  3. Use proper indentions and empty lines for block separation
  4. Don’t use magic numbers like “81” and “9”. Create constants. Give them names! make them dependent from each other if they are dependent.

solved Segmentation Fault C++ pointers [closed]