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 < tam_ && !found){
//touch tam_ in the constructor, is the size of Celda
if(dni == bloques_[i]){
found = true;
pos = i;
}
++i;
}
return pos;
}
主要我調用另一個類的方法,調用其他類使用我在這裏複製的外觀方法。在某些情況下,它可以工作,但其他時候程序會停止提供分段錯誤。C++奇怪的分段錯誤
當我使用調試器時,我創建了另一個用於存儲tam_值(tam_是int類型)的變量,並且當我到達該行或while循環(帶有tam_的條件)時,有時會出現分段錯誤。
Celda的構造是:
Celda :: Celda(int tamanio)
{
bloques_ = new int[tamanio];
bloq_ocupados_ = 0;
tam_ = tamanio;
for(int i = 0 ; i < tam_ ; ++i){
bloques_[i] = 0;
}
}
Tabla :: Tabla(int numcel, int tambloq)
{
nceldas_ = numcel;
tam_bloque_ = tambloq;
tabla_ = new Celda*[nceldas_];
for(int i = 0 ; i < nceldas_ ; ++i){
tabla_[i] = new Celda(tam_bloque_);
}
ocupadas_ = 0;
}
class Celda
{
private:
int* bloques_;
int bloq_ocupados_;
int tam_;
和向下的公共部分
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]->gettam();
while(!encontrado && comparaciones < tamaniotab){
pos_dentro = tabla_[pos]->buscar(dni);
if(pos_dentro != -1){ //si lo encuentro...
encontrado = true;
pos_fuera = pos;
comparaciones += pos_dentro + 1;
}else if(pos < nceldas_ - 1){ //mientras no nos salgamos de la tabla, avanzamos
++pos;
comparaciones += tabla_[0]->gettam();
}else {
pos = 0; //si nos salimos, volvemos al principio
comparaciones += tabla_[0]->gettam();
}
}
return comparaciones;
}
使用英文標識符被認爲是一種很好的做法。只是一個offtopic筆記。更多ontopic:我們可以有一個測試用例嗎? http://sscce.org/ – Griwes 2012-03-26 16:35:42
你可以發佈類'Celda'的析構函數,複製構造函數和賦值運算符嗎? – hmjd 2012-03-26 16:37:56
爲了在條件中發生段錯誤,你必須有一個糟糕的'this'指針。你能否檢查一下'這個'在碰撞的時候是否有意義? – Arkadiy 2012-03-26 16:39:01