Access Violation

I wrote this code where there is no error on compiling.Though when I'm about to run it I get an access violation error and I have no idea why.

There is something wrong with the pointer s1 but can't figure out what.


#include
#include
#include

using namespace std;

class student{
private:
int am;
char* oe;
int les;
float *lesg;
float mo;
int ples;
int nples;
float mingr;
float maxgr;
public:
student(int num2,float *grad,int id,char* onoma){ //constructor of student
if(num2>0 && num2<9) les=num2;
else les=0;
lesg=new float[les];//desmeusi mnhmhs
if (lesg!=0){
for (int i=0; i<les; i++)
if (grad[i]>0.0 && grad[i]<10.1)lesg[i] = grad[i];
else lesg[i]=0;
};
setid(id);
setoe(onoma);
mo=0;
ples=0;
nples=0;
mingr=0;
maxgr=0;
};
~student(){ //destructor of student
delete [] lesg;
};
void setid(int id){ //setting id
if (id>0 && id<10000) am=id;
else am=0;};
void setoe(char* onoma){ //setting student's name
if (onoma!=0 && strlen(onoma)>0 && strlen(onoma)<41) oe=onoma; //elegxos gia xaraktires
else oe="NO_NAME";};
float setmo();
int setples();
int setnples();
float setmax();
float setmin();
void printdata();
student(const student &s1); //copy constructor
};

float student::setmo()//calculating average
{
float sum=0;
int i;
for(i=0;i<les;i++){
sum+=lesg[i];
}
return sum/les;
}

int student::setples() // calculating passed lessons
{
int sum=0;
int i;
for(i=0;i<les;i++){
if(lesg[i] > 5.0) sum+=1;
}
return sum;
}

int student::setnples()// calculating not passed lessons
{
int sum=0;
int i;
for(i=0;i max) max=lesg[i];
}
return max;
}

float student::setmin()// calculating minimum grade
{
float min=lesg[1];
int i;
for (i=1;i=0 && strlen(title)<81) dtitle=title;
if(v_diplom>=0.0 && v_diplom<=10.0) dipgr=v_diplom;
if(w_factor>=0 && w_factor<=1) f=w_factor;
};
void get_fingrad();
void printdata();
};

void grad_student::get_fingrad(){
fingrad=student::setmo()*(1-f)+dipgr*f;
}

void grad_student::printdata(){
student::printdata();
cout<<"
Diplomatic Project Title:"<<dtitle<<endl;
cout<<"
Diplomatic Project Grade:"<<dipgr<<endl;
cout<<"
Diplomatic Project Grade Factor:"<<f<<endl;
cout<<"
Final Grade:"<<fingrad<<endl;
}

int main(){

float gr[]={7.0, 3.0, 6.0, 10.0, 4.0}; //grade array
student s1(sizeof(gr)/sizeof(int),gr,1510,"James Stathatos");//initialization of s1

grad_student gs1(s1,"Telecommunications",7.3,0.4);//initiallization of gs1

gs1.get_fingrad();
gs1.printdata();

s1.~student();

system("pause");
}


Thank you in advance and have a Happy New Year!:)

Comments

  • It's because of:
    [code]
    student::student(const student &s1)
    {
    ...
    for(i=0;i<strlen(oe);i++) oe[i]=s1.oe[i];
    ...
    }
    [/code]

    oe for the copy constructor is trying to write character to character from the existing sting in s1 to a still uninitialized string in the current initializing class.

    oe is simply a bad pointer. You might try something like the following instead:
    [code]
    oe = new char[strlen(s1.oe)+1];
    memset(oe, '', strlen(s1.oe)+1);
    memcpy(oe, s1.oe, strlen(s1.oe));
    [/code]

    Also, in general (as there are very few exceptions) you should never explicitly call a destructor. This will automatically be called when the scope of the variable expire. Which in the case of your code will cause a double delete on s1.lesg.
  • It's because of:
    [code]
    student::student(const student &s1)
    {
    ...
    for(i=0;i<strlen(oe);i++) oe[i]=s1.oe[i];
    ...
    }
    [/code]

    oe for the copy constructor is trying to write character to character from the existing sting in s1 to a still uninitialized string in the current initializing class.

    oe is simply a bad pointer. You might try something like the following instead:
    [code]
    oe = new char[strlen(s1.oe)+1];
    memset(oe, '', strlen(s1.oe)+1);
    memcpy(oe, s1.oe, strlen(s1.oe));
    [/code]

    Also, in general (as there are very few exceptions) you should never explicitly call a destructor. This will automatically be called when the scope of the variable expire. Which in the case of your code will cause a double delete on s1.lesg.
  • Thanks a lot it just worked fine now!
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories