samedi 16 novembre 2019

EXC_BAD_ACCESS pointer C++

This is by far the most frustrating error I have come across while programming in C++. Here is the code I am trying to run. Every time I run it, it gives me bad access error and when I try to debug it with LLDB, it shows that value of every member in end = 0 and gives the bad access pointer error. Please let me know what's the solution to the current problem. I have been stuck on this for a while now and I come across this error every now and then so any debugging tips would be appreciated.

priority_queue <node *, vector<node*>, compPair> pq;
  vec[here]->prio = 0;
  pq.push(vec[here]);

  while(!pq.empty()){
    node* place1 = pq.top();pq.pop();
    for(int i=0;i<place1->r.size();i++){
      edge* rd = place1->r[i];
      node* place2 = rd->x;
      if(place2==place1){
        place2=rd->y;
      }
      if(place2->prio == -2){
        place2->prio = place1->prio + rd->dist;
        pq.push(place2);
      }
      else if (place2->prio > (place1->prio + rd->dist)){
        place2->prio = place1->prio + rd->dist;
      }
    }
  }

  node* beg = vec[here];
  node* end = vec[there];

  if(vec[here]->prio < 0 || vec[there]->prio < 0){
    cout << "There is no path in the graph between the 2 desired places" << endl;
    return;
  }
  node* lowp = end;
  vector <pathh*> p;
  while(beg!=end){
    double reallowp = end->prio;
    edge* rtt = end->r[0];
    for(int i =0;i<end->r.size();i++){
      edge *r = end->r[i];
      node* town1 = r->x;
      if(town1 == end){
        town1 = r->y;
      }
      if(town1->prio <= reallowp){
        reallowp = town1->prio;
        lowp = town1;
        rtt = r;
      }
    }

    pathh* d = new pathh(end->id,lowp->id,rtt->dist,rtt->name);

    p.push_back(d);
    end=lowp;
  }

Aucun commentaire:

Enregistrer un commentaire