Hi, use Eclipse to debug ns2 c++ parts. You will save a lot of time and resolve problems like this one very easy.
Some webpages that give instructions about this : http://wcms1.rz.tu-ilmenau.de/fakia/NS2-for-Eclipse.6025.0.html http://blog.karthiksankar.com/ns2-eclipse/ ns22sn wrote: > > Hi, I am a beginner in learning C/C++. I was trying to run a network > simulation code and encounter error "segmentation fault". I cant figure > out how to solve it. I can only find the function with error, but have no > idea what is going on there. The error is in the function "VanetProp::Pr". > I used Ubuntu 10.10 and NS-2.34 for this running. Please help me to find > our the error. i paste source code for C++ source code and Header file. > Thanks. > > Code for header file: > > #ifndef V2VDSRC_H > #define V2VDSRC_H > > #include <wireless-phy.h> > #include <packet-stamp.h> > #include <propagation.h> > #include <rng.h> > > #define BUFF_SIZE 4096 > #define SUBURBAN 0 > #define HIGHWAY 1 > #define RURAL 2 > > class VanetProp : public Propagation { > public: > VanetProp(); > virtual double Pr(PacketStamp *t, PacketStamp *r, WirelessPhy > *ifp); > double dualSlope(PacketStamp *t, PacketStamp *r, double lambda, > double > L, double &dist, double &scatter); > virtual int command(int argc, const char*const* argv); > //get interference distance > virtual double getDist(double Pr, double Pt, double Gt, double > Gr, > double hr, double ht, double L, double lambda); > ~VanetProp(); > > private: > RNG *ranVar; > int seed; > int initialized; > > //pathloss exponents for the large-scale model > double gamma1_suburban, gamma2_suburban, gamma1_highway, > gamma2_highway, > gamma1_rural, gamma2_rural; > //the standard deviation for the log normal shadowing > double std_db1_suburban, std_db2_suburban, std_db1_highway, > std_db2_highway, std_db1_rural, std_db2_rural; > //the critical distance for the dual slope model > double dc_suburban, dc_highway, dc_rural; > > double **envelop_data; //this array is used to load > data from the file > int a_col; //number of spectrums > double **node_time_index; //those 2 variables are used to > store the > previous > double **node_sched_time; //time for nodes. > double fs; //sampling rate of data in the > file > int N; //number of samples in file > double fm0; //maximum doppler frequency in > file > double fm; //maximum doppler frequency in > scenario > int max_node_id; > int environment; /* = 0 suburban > = 1 highway > = 2 rural > */ > int d0; // the reference distance > BaseTrace *pwr_tracer; > void trace(double index, double dist, double pl, double > fad_prw, double > scatter); > int loadFile(const char *file_name); > double Interpolate(int, int, int a_ind); > }; > > #endif > > Source code for C++ file > > #include <iostream> > #include <fstream> > #include <string> > #include <math.h> > > #include <antenna.h> > #include <vanetprop.h> > #include <ctype.h> > > > > static class VanetPropClass: public TclClass { > public: > VanetPropClass() : TclClass("Propagation/VanetProp") {} > TclObject* create(int, const char*const*) { > return (new VanetProp); > } > } class_VanetProp; > > > VanetProp::VanetProp() : Propagation() > { > pwr_tracer = 0; > initialized = 0; > N = fm = fs = 0; > a_col = 0; > node_time_index = new double*[1]; > node_time_index[0] = new double[1]; > node_sched_time = new double*[1]; > node_sched_time[0] = new double[1]; > > /******set some values by default********/ > d0 = 1.0; //set reference distance d0 = 1 m; > environment = SUBURBAN; //set default environment to suburban > /***************************************/ > > bind("gamma1_suburban_", &gamma1_suburban); > bind("gamma2_suburban_", &gamma2_suburban); > bind("std_db1_suburban_", &std_db1_suburban); > bind("std_db2_suburban_", &std_db2_suburban); > bind("dc_suburban_", &dc_suburban); > > bind("gamma1_highway_", &gamma1_highway); > bind("gamma2_highway_", &gamma2_highway); > bind("std_db1_highway_", &std_db1_highway); > bind("std_db2_highway_", &std_db2_highway); > bind("dc_highway_", &dc_highway); > > bind("gamma1_rural_", &gamma1_rural); > bind("gamma2_rural_", &gamma2_rural); > bind("std_db1_rural_", &std_db1_rural); > bind("std_db2_rural_", &std_db2_rural); > bind("dc_rural_", &dc_rural); > > bind("seed_", &seed); > > ranVar = new RNG; > ranVar->set_seed(RNG::PREDEF_SEED_SOURCE, seed); > } > > VanetProp::~VanetProp() > { > int i; > for(i = 0; i < N; ++i){ > delete [] envelop_data[i]; > envelop_data[i] = 0; > } > delete [] envelop_data; > envelop_data = 0; > > for(i = 0; i < max_node_id; ++i){ > delete [] node_time_index[i]; > delete [] node_sched_time[i]; > node_time_index[i] = 0; > node_sched_time[i] = 0; > } > delete [] node_time_index; > delete [] node_sched_time; > node_time_index = 0; > node_sched_time = 0; > } > > int VanetProp::command(int argc, const char* const *argv) > { > TclObject *obj; > int file_loadin; > int i, j; > > if(argc == 3){ > if(!strcmp(argv[1], "Tracer")){ > if( (obj = TclObject::lookup(argv[2])) == 0) { > fprintf(stderr, "Propagation: %s lookup of %s > failed\n", argv[1], > argv[2]); > return TCL_ERROR; > } > pwr_tracer = (BaseTrace *) obj; > assert(pwr_tracer); > return TCL_OK; > } > else if(!strcmp(argv[1], "LoadPropFile")){ > if((file_loadin = loadFile(argv[2])) == TCL_OK) > initialized = 1; > return file_loadin; > } > else if(!strcmp(argv[1], "Environment")){ > if(!strcmp(argv[2], "Suburban")) environment = SUBURBAN; > else if(!strcmp(argv[2], "Highway")) environment = > HIGHWAY; > else if(!strcmp(argv[2], "Rural")) environment = RURAL; > else{ > fprintf(stderr, "The specified environment does > not exist. \nOptions: > suburban, highway, or rural."); > return TCL_ERROR; > } > return TCL_OK; > } > else if(!strcmp(argv[1], "MaxNodeID")){ > max_node_id = atoi(argv[2]); > delete node_time_index; > node_time_index = 0; > > node_time_index = new double*[max_node_id+1]; > node_sched_time = new double*[max_node_id+1]; > > for(i = 0; i < max_node_id; ++i){ > node_time_index[i] = new double[max_node_id+1]; > node_sched_time[i] = new double[max_node_id+1]; > for(j = 0; j < max_node_id; ++j){ > node_time_index[i][j] = -1; > node_sched_time[i][j] = -1; > } > } > return TCL_OK; > } > } > return Propagation::command(argc, argv); > } > > int VanetProp::loadFile(const char *file_name) > { > string buff; > char *tkns; > char cbuff[BUFF_SIZE]; > char arg1[BUFF_SIZE]; > char arg2[BUFF_SIZE]; > int found_data = 0; > int i = 0, j = 0; > > //open the specified file > ifstream fin(file_name); > > if(!fin.is_open()){ > printf("%s: File cannot be opened.", file_name); > return TCL_ERROR; > } > > while(!fin.eof()) > { > getline(fin, buff); > > if( (buff[0] != '#') && !buff.empty()) > { > strcpy(cbuff, buff.c_str()); > if(!found_data) { > sscanf(cbuff, "%s %s", arg1, arg2); > > if(!strcmp(arg1,"fm")){ > fm0 = atof(arg2); > } > else if(!strcmp(arg1, "a_col")){ > a_col = atoi(arg2); > } > else if(!strcmp(arg1,"N")){ > N = atoi(arg2); > envelop_data = new double*[N]; > } > else if(!strcmp(arg1,"fs")){ > fs = atof(arg2); > } > > else if(!strcmp(arg1, "DATA")){ > found_data = 1; > printf("Data found.\n"); > } > else{ > printf("Error in input file.\n"); > return TCL_ERROR; > } > } > else{ > envelop_data[i] = new double[a_col]; > tkns = strtok(cbuff, " \t\n\r"); > j = 0; > while(tkns != NULL) { > envelop_data[i][j++] = atof(tkns); > tkns = strtok(NULL, " \t\n\r"); > > } > ++i; > } > } > } > fin.close(); > return TCL_OK; > } > > double VanetProp::dualSlope(PacketStamp *t, PacketStamp *r, double lambda, > double L, double &dist, double &scatter) > { > double tx, ty, tz; //transmitter coordinates > double rx, ry, rz; //receiver coordinates > double gamma1, gamma2, std_db1, std_db2, dc; > > switch(environment){ > case 0: gamma1 = gamma1_suburban; > gamma2 = gamma2_suburban; > std_db1 = std_db1_suburban; > std_db2 = std_db2_suburban; > dc = dc_suburban; > break; > case 1: gamma1 = gamma1_highway; > gamma2 = gamma2_highway; > std_db1 = std_db1_highway; > std_db2 = std_db2_highway; > dc = dc_highway; > break; > case 2: gamma1 = gamma1_rural; > gamma2 = gamma2_rural; > std_db1 = std_db1_rural; > std_db2 = std_db2_rural; > dc = dc_rural; > break; > default: gamma1 = 0.0; > gamma2 = 0.0; > std_db1 = 0.0; > std_db2 = 0.0; > dc = 100.0; > break; > } > t->getNode()->getLoc(&tx, &ty, &tz); > r->getNode()->getLoc(&rx, &ry, &rz); > > //add the relative distance of antenna to the node > rx += r->getAntenna()->getX(); > ry += r->getAntenna()->getY(); > rz += r->getAntenna()->getZ(); > tx += t->getAntenna()->getX(); > ty += t->getAntenna()->getY(); > tz += t->getAntenna()->getZ(); > > double dX = rx - tx; > double dY = ry - ty; > double dZ = rz - tz; > dist = sqrt(dX * dX + dY * dY + dZ * dZ); //distance between > receiver and > transmitter > > scatter = 0; > // get antenna gain > double Gt = t->getAntenna()->getTxGain(dX, dY, dZ, lambda); > double Gr = r->getAntenna()->getRxGain(dX, dY, dZ, lambda); > > double P0 = Friis(t->getTxPr(), Gt, Gr, lambda, L, d0); //use the > freepath equation to get power (in dB) at distance do; > > //calculate the critical distance > //double dc = (4 * tz * rz)/lambda; > double pl; > > //get the longterm average power > if(dist <= d0){ > pl = 10*log10(Friis(t->getTxPr(), Gt, Gr, lambda, L, dist)); > if(!initialized) scatter = ranVar->normal(0.0, std_db1); > } > else if(dist <= dc){ > pl = 10*log10(P0) - 10*gamma1*log10(dist/d0); > if(!initialized) scatter = ranVar->normal(0.0, std_db1); > } > else { > pl = 10*log10(P0) - 10*gamma1*log10(dc/d0) - > 10*gamma2*log10(dist/dc); > if(!initialized) scatter = ranVar->normal(0.0, std_db2); > > } > return (pl+scatter); > } > > //The error seems to be in this function. > double VanetProp::Pr(PacketStamp *t, PacketStamp *r, WirelessPhy *ifp) > { > double L = ifp->getL(); //system loss > double lambda = ifp->getLambda(); //wavelength > double dist = 0; > double K; > double m; > double x, y; > double alpha, beta; > double v1, v2, a; > double scatter = 0; > double pl = pow(10,dualSlope(t, r, lambda, L, dist, scatter)/10); > double fad_pwr = 1; > int n1id = 0, n2id = 0, tmp; > > //find the small scale fading > > if(initialized) > { > n1id = t->getNode()->nodeid(); > n2id = r->getNode()->nodeid(); > > //Find the maximum of the two and make n1 always higher > if (n1id < n2id) { > tmp = n1id; > n1id = n2id; > n2id = tmp; > } > //set the starting point of the current channel > if(node_time_index[n1id][n2id] == -1.0){ > if (max_node_id) { > > node_time_index[n1id][n2id] = > int(floor(double((n1id-1)*N)/double(max_node_id) + > > double((n2id)*N)/double(max_node_id*max_node_id))); > node_sched_time[n1id][n2id] = 0; > } > else{ > node_time_index[n1id][n2id] = 0; > node_sched_time[n1id][n2id] = 0; > } > } > > v1 = t->getNode()->speed(); > v2 = r->getNode()->speed(); > > if( v1 == 0.0 && v2 == 0.0) a = 0; > else a = (v2 > v1) ? (v1/v2) : (v2/v1); > > fm = (v2 > v1) ? (v2/lambda) : (v1/lambda); //get the > doppler spread > > node_time_index[n1id][n2id] += (Scheduler::instance().clock() - > node_sched_time[n1id][n2id])*fs*fm/fm0; > node_time_index[n1id][n2id] -= > double(N)*floor(node_time_index[n1id][n2id]/double(N)); > node_sched_time[n1id][n2id] = Scheduler::instance().clock(); > > //the parameters to get the nakagami envelop > m = -1.3*log10(dist/d0) + 3.7; > > int a_ind = a/0.10 + 0.5; > > //get quadrature and in phase components > x = Interpolate(n1id, n2id, a_ind); > y = Interpolate(n1id, n2id, 11+a_ind); > x = x ? x : 0.000001; > y = y ? y : 0.000001; > > //calculate the power envelop > if(m > 1){ > K = m - 1.0 + sqrt(m*m - m); > x += sqrt(2*K); > fad_pwr = (x*x + y*y)/(2*(K+1)); > } > else{ > if(m < 0.5) m = 0.5; > alpha = m + sqrt(m*(1-m)); > beta = 2*m - alpha; > fad_pwr = (alpha*x*x + > beta*y*y)/(alpha*alpha+beta*beta); > } > } > > trace(node_time_index[n1id][n2id], dist, pl, fad_pwr, scatter); > > return pl*fad_pwr; > } > > > double VanetProp::Interpolate(int n1id, int n2id, int a_ind) > { > double interp; > double X0, X1, X2, X3; > int ind0, ind1, ind2, ind3; > > ind1 = int(floor(node_time_index[n1id][n2id])); > ind0 = (ind1-1+N) % N; > ind2 = (ind1+1) % N; > ind3 = (ind1+2) % N; > > X1 = node_time_index[n1id][n2id] - ind1; > X0 = X1+1.0; > X2 = X1-1.0; > X3 = X1-2.0; > > interp = envelop_data[ind0][a_ind]*X1*X2*X3/(-6.0) + > envelop_data[ind1][a_ind]*X0*X2*X3*(0.5) + > envelop_data[ind2][a_ind]*X0*X1*X3*(-0.5) + > envelop_data[ind3][a_ind]*X0*X1*X2/6.0; > return interp; > } > > void VanetProp::trace(double index, double dist, double pl, double > fad_pwr, double scatter) > { > if(pwr_tracer) { > sprintf(pwr_tracer->buffer(), "%f \t %f \t %f \t %f \t %f \t > %f", > index, Scheduler::instance().clock(), dist, 10*log10(pl), > 10*log10(fad_pwr), scatter); > pwr_tracer->dump(); > } > } > > double VanetProp::getDist(double Pr, double Pt, double Gt, double Gr, > double hr, double ht, double L, double lambda) > { > return sqrt(sqrt(Pt * Gt * Gr * (hr * hr * ht * ht) / Pr)); > } > > > -- View this message in context: http://old.nabble.com/ns-2-segmentation-fault-error-help-tp31415886p31419738.html Sent from the ns-users mailing list archive at Nabble.com.
