Note that there are some explanatory texts on larger screens.

plurals
  1. POc++ weird segmentation fault
    text
    copied!<pre><code>int Celda :: look(int dni) { bool found = false; int i = 0; int pos = -1; //tam_ sometimes have a strange value, but I only while(i &lt; tam_ &amp;&amp; !found){ //touch tam_ in the constructor, is the size of Celda if(dni == bloques_[i]){ found = true; pos = i; } ++i; } return pos; } </code></pre> <p>In main I call a method of another class that calls other that uses the look method I've copied here. In some cases it works, but other times the program stops giving a segmentation fault.</p> <p>When I use a debugger, I've created another variable for store the tam_ value (tam_ is int type), and when I reach that line or the while loop (with condition with tam_) sometimes the segmentation fault appears.</p> <p>The constructor of Celda is:</p> <pre><code>Celda :: Celda(int tamanio) { bloques_ = new int[tamanio]; bloq_ocupados_ = 0; tam_ = tamanio; for(int i = 0 ; i &lt; tam_ ; ++i){ bloques_[i] = 0; } </code></pre> <p>}</p> <pre><code>Tabla :: Tabla(int numcel, int tambloq) { nceldas_ = numcel; tam_bloque_ = tambloq; tabla_ = new Celda*[nceldas_]; for(int i = 0 ; i &lt; nceldas_ ; ++i){ tabla_[i] = new Celda(tam_bloque_); } ocupadas_ = 0; } class Celda { private: int* bloques_; int bloq_ocupados_; int tam_; </code></pre> <p>And down the public section</p> <pre><code>int Tabla :: busq_lineal(int dni) //si pos_dentro acaba valiendo -1, no se encontró { bool encontrado = false; int pos = hash(dni), comparaciones = 0, pos_dentro, pos_fuera; int tamaniotab = nceldas_ * tabla_[0]-&gt;gettam(); while(!encontrado &amp;&amp; comparaciones &lt; tamaniotab){ pos_dentro = tabla_[pos]-&gt;buscar(dni); if(pos_dentro != -1){ //si lo encuentro... encontrado = true; pos_fuera = pos; comparaciones += pos_dentro + 1; }else if(pos &lt; nceldas_ - 1){ //mientras no nos salgamos de la tabla, avanzamos ++pos; comparaciones += tabla_[0]-&gt;gettam(); }else { pos = 0; //si nos salimos, volvemos al principio comparaciones += tabla_[0]-&gt;gettam(); } } return comparaciones; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload