Reputation: 181
I have been trying to get rid of this error but nothing seems to work. I am coding in C++ in Visual Studio 2012, using OpenGL. Here I initialize the rtScene
object, the default constructor of this object reads a file and sets the other data member values.
The error pops up when I make the call to function SphereSet()
, which sets the values of data members of sphere using respective functions of data members.
class rtPoint
{
friend class Sphere;
friend class rtScene;
private:
double xx,yy,zz;
public:
rtPoint():xx(0),yy(0),zz(0){cout<<"vi";};
void rtPointSet(double x,double y,double z)
{
xx=x;yy=y;zz=z;
//cout<<xx<<" "<<yy<<" "<<zz<<"\n";
}
void rtPointSet(rtPoint p)
{
xx=p.xx;
yy=p.yy;
zz=p.zz;
}
};
class Sphere
{
friend class rtScene;
private:
rtPoint center;
double radius;
color3 color;
Material surface;
public:
Sphere():center(),radius(1),color(),surface(){};
void SphereSet(double x,double y,double z,double r,
double rr,double gg,double bb,double Par,
double Pag,double Pab, double Pdr, double Pdg,
double Pdb,double Psr,double Psg,double Psb,double PhCo)
{
center.rtPointSet(x,y,z);
color.set(rr,gg,bb);
radius=r;
surface.MaterialSet(Par, Pag, Pab, Pdr, Pdg, Pdb, Psr, Psg, Psb, PhCo);
}
};
class rtScene
{
private:
Sphere *Sph_obj;
//Sphere Sph_obj[3];
int nS,nL;
rtCamera CAM;
rtLight *Lights;
public:
rtScene(char *fname)
{
FILE *fp;
fp=fopen(fname,"r");
double a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q;
char ch;
//.........read camera.........//
fscanf(fp,"%lf,%lf,%lf,%lf,%lf,%lf\n",&a,&b,&c,&d,&e,&f); // 6 values
this->CAM.rtCameraWindow(a,b,c,d,e,f);
fscanf(fp,"%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf\n",&a,&b,&c,&d,&e,&f,&g,&h,&i); // 9 values
this->CAM.rtCameraSet(a,b,c,d,e,f,g,h,i);
//.............................//
//.......read Lights.............//
fscanf(fp,"%c %d\n",&ch,&nL);
cout<<nL;
if(ch=='l')
{
this->Lights=new rtLight[nL];
for(int i=0;i<nL;i++)
{
fscanf(fp,"%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf\n",&a,&b,&c,&d,&e,&f,&g,&h,&i,&j,&k,&l); //12 values
this->Lights[i].rtLightSet(a,b,c,d,e,f,g,h,i,j,k,l);
}
}
//..............................//
//.....read spheres.............//
fscanf(fp,"%c %d\n",&ch,&nS);
cout<<nS;
if(ch=='s')
{
this->Sph_obj=new Sphere[nS];
//Sph_obj=(class Sphere *) malloc(sizeof(Sphere)*nS);
for(int i=0;i<nS;i++)
{
fscanf(fp,"%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf\n",&a,&b,&c,&d,&e,&f,&g,&h,&i,&j,&k,&l,&m,&n,&o,&p,&q);
this->Sph_obj[i].SphereSet(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q);
}
}
//.............................//
}
};
code crashes giving the error "First-chance exception at 0x010A85FB in ray tracer.exe: 0xC0000005: Access violation writing location 0x709DF990" when function rtPointSet
tries to set the xx
, yy
, zz
value for center variable of Sphere.
Upvotes: 0
Views: 295
Reputation: 66459
You're overwriting the loop variable.
for(int i=0;i<nS;i++) // Loop over 'i'
{
fscanf(fp, [...],&i,[...]); // Oops!
this->Sph_obj[i].SphereSet([...]; // Bang!
}
Declaring variables close to their use and not reusing them is a good thing - if you had declared a
..q
inside the loop, the compiler would have caught that error for you.
Upvotes: 1