Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Access Violation

Status
Not open for further replies.

csripriya1

Programmer
May 13, 2003
35
US
Hi all,
I have a problem with this access violation.A function in my class reads in
a bunch of values.My program crashes towards the end of this function.It prints
NINFOLINE correctly ,waits for the input(test) then dies after taking the input
for test.When it crashes,it gives an error message of access violation.I am wondering
what would have gone wrong in the end?

The program crashed for few input files ,but worked fine for other input files.
In both the cases the last line INFOLINE was printed and the input for test was taken.
I have given my code below.Can anyone help me in this regard.Thanks for your time.

#include "sdf_class.h"


void sdfcomp::getData(char *stra)
{
char dummy[80];
char *chk=NULL;
int in;

int test;

ifstream inpfile(stra,ios::in);
if(!inpfile)
{
cerr<<&quot;File could not be opened&quot;<<endl;
exit(1);
}

inpfile>>natoms;
xcrd=new float[natoms];
ycrd=new float[natoms];
zcrd=new float[natoms];

assert(xcrd!=0);
assert(ycrd!=0);
assert(zcrd!=0);

in=1;
while(in<=natoms)
{
inpfile>>xcrd[in]>>ycrd[in]>>zcrd[in];
in++;
}
in=0;
chk=&quot;$$$$&quot;;
inpfile.getline(dummy,80,'\n');
while(strcmp(dummy,chk)!=0 )
{
cout<<&quot;string tbc:&quot;<<dummy<<endl;
strcpy(dum[in],dummy);
inpfile.getline(dummy,80,'\n');
in ++;
}
ninfoline=in;
cout<<&quot;NINFOLINE:&quot;<<in<<endl;
cin>>test;
}
 
while(in[red]<=[/red]natoms)
{
inpfile>>xcrd[in]>>ycrd[in]>>zcrd[in];
in++;
}


My first guess would be that when in == natoms you are writing to memory ( xcrd[in] and ycrd[in] and zcrd[in]) that you have not allocated and therefore have corrupted memory.

-pete
 
Thanks palbano,
What a careless mistake,I had made.Really thanks a lot for your time and help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top