Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7708111
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:31:38+00:00 2026-06-01T00:31:38+00:00

int Celda :: look(int dni) { bool found = false; int i = 0;

  • 0
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;
}

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.

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.

The constructor of Celda is:

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_;

And down the public section

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T00:31:40+00:00Added an answer on June 1, 2026 at 12:31 am

    The error is most probably in this line:

    int pos = hash(dni);
    

    As you said, your hash function just returns dni % 199. This will work okay, only if you have at least 200 items in your hash table.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

int somefunction(bool a) { try { if(a) throw Error(msg); return 2; } catch (Error
int? x = null; x = x + 1; // Works, but x remains
class Celda { private: int* bloques_; public: Celda(int size); ~Celda(){ delete [] bloques_; };
int x = n / 3; // <-- make this faster // for instance
int x; printf(hello %n World\n, &x); printf(%d\n, x);
int[] mylist = { 2, 4, 5 }; IEnumerable<int> list1 = mylist; list1.ToList().Add(1); //
int n = 5; for(int i = 0;i!=n;i++)//condition != { //executing 5times } int
int main(void) { char tmp, arr[100]; int i, k; printf(Enter a string: ); scanf_s(%s,
int main() { int var = 0;; // Typo which compiles just fine }
int main(void) { std::string foo(foo); } My understanding is that the above code uses

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.