質問

I'm getting a strange segfault on my code (see below) when the loop arrives on the 81o iteration, and I have no idea why it's happening. I used valgrind, but it spilled an incomprehensible error, which I supose that comes from stl, resulting in reading a invalid memory adress. A while before, on the 49o iteration, it acused an warning that there was an adress 48 bytes inside one of block of 64 bytes free'd.

It ends saying that "0x8 is not stack'd, malloc'd or (recently) free'd", and the line where it happens the segfault is the line 78 of the paste, where el is acessed for the first time inside de loop, so it appears that I should be hitting the end of the queue, but it's not the case, due to line 74.

#include <iostream>
#include <fstream>
#include <cmath>
#include <string>
#include <vector>
#include <deque>
#include <gsl/gsl_math.h>
#include <gsl/gsl_monte.h>
#include <gsl/gsl_monte_plain.h>
#include <gsl/gsl_monte_miser.h>
#include <gsl/gsl_monte_vegas.h>

using namespace std;

const int D=1;

double f(double *x,size_t n,void *params){
  if(-1. <= x[0] && x[0] <= 1. )
    return 0.75*(1.-(x[0]*x[0]));
  else
    return 0;
}

double g(double *x,size_t n,void *params){
  if(-1. <= x[0] && x[0] <= 1. )
    return 1.5*(x[0]*x[0]);
  else
    return 0;
}
typedef struct domain{
    int good;
    double xi[D],xf[D];
    double S;
} domain;

int main(){
  int i,j,k,counter=0;
  double cutoff=0.025,S,err;
  domain mdel,tdel;
  deque <double> lside,rside;
  deque <double> :: iterator lel,rel;
  deque <domain> dom;
  deque <domain> :: iterator el;
  //double xi[D], xf[D];
  double xi,xf;
  size_t calls = 500000;
  gsl_monte_function F;
  const gsl_rng_type *T;
  gsl_rng *r;
  //gsl_monte_vegas_state *s = gsl_monte_vegas_alloc (D);
  gsl_monte_miser_state *s_m=gsl_monte_miser_alloc (D);

  T = gsl_rng_default;
  r = gsl_rng_alloc (T);

  F.f = &f;
  F.dim=D;
  F.params=NULL;

  mdel.good=0;
  mdel.xi[0]=-1.;
  mdel.xf[0]=1.;
  mdel.S=1.0;

  //lside.push_back(-1.0);
  //rside.push_back(1.0);

  dom.push_back(mdel);

  //while()
  for(el=dom.begin();el!=dom.end();el++){
    //for(i=0;i<D;i+=1){xi[i]=el->xi[i];xf[i]=el->xf[i];}
    S=0.0;
    if(el==dom.end())
      cout<< "end of deque\n";
    //xi[0]=el->xi[0];
    //xf[0]=el->xf[0];
    xi=el->xi[0];
    xf=el->xf[0];
    //xi=*lel;
    //xf=*rel;

    gsl_monte_miser_integrate(&F,&xi,&xf,D,calls,r,s_m,&S,&err);

    //gsl_monte_vegas_integrate (&F, &xi, &xf, D, 10000, r, s,&S, &err);
    //do{
    //  gsl_monte_vegas_integrate (&F, &xi, &xf, D, calls/5, r, s,&S, &err);
    //}while (fabs (gsl_monte_vegas_chisq (s) - 1.0) > 0.5);

    el->S=S;
    cout << "size & maxsize:  " << dom.size() << "  "<< dom.max_size() << "\n";
    cout<< "counter @ " << counter << " ";
    //cout << "[" << xi[0] << "," << xf[0] << "] : ";
    cout << "[" << xi << "," << xf << "] : ";
    cout << "S= " << S << " +- " << err << "  :  " << 0.75*(xf-xi-(xf*xf*xf-xi*xi*xi)/3.0) <<"\n\n";

    if(S > cutoff){
      el->good=1;

      tdel.good=0;
      tdel.xi[0]=el->xi[0];
      tdel.xf[0]=(el->xi[0]+el->xf[0])/2.;
      tdel.S=0.0;

      dom.push_back(tdel);
      //lside.push_back(tdel.xi[0]);
      //rside.push_back(tdel.xf[0]);

      tdel.good=0;
      tdel.xi[0]=(el->xi[0]+el->xf[0])/2.;
      tdel.xf[0]=el->xf[0];
      tdel.S=0.0;

      dom.push_back(tdel);
      //lside.push_back(tdel.xi[0]);
      //rside.push_back(tdel.xf[0]);
    }

    counter++;
  }

  //gsl_monte_vegas_free (s);
  gsl_monte_miser_free (s_m);
  gsl_rng_free (r);

  return 0;
}

If anyone could help me with this, I will be very gratefull

p.s.: the code does not require any input, so it's just compile and link it against the GSL

役に立ちましたか?

解決

You are invalidating the el iterator when you add elements to the deque dom in the middle of the for loop.

From the question Iterator invalidation rules:

Insertion

Sequence containers

  • vector: all iterators and references before the point of insertion are unaffected, unless the new container size is greater than the previous capacity (in which case all iterators and references are invalidated) [23.2.4.3/1]
  • deque: all iterators and references are invalidated, unless the inserted member is at an end (front or back) of the deque (in which case all iterators are invalidated, but references to elements are unaffected) [23.2.1.3/1]
  • list: all iterators and references unaffected [23.2.2.3/1]
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top