memory acses violation

i'm new to pointer in c++ , can someone help me solve this

class student{
public:
int id;
student(int id){
this->id= id;
}

void somefunction(student* stud,int id)
{
stud= new student(id);
}
}

//anathoer function
int* otherint(int num)
{

return new int(num);

}

int main(){
student* erik= new student(3);
student* jacob=nullptr;



//but this fine why?

int* num= otherint(23);

std::cout<<*num;

//this following statement i got exception

erik->somefunction(jacob, 2);
std::cout<<jacob->id;

return 0;


}
Last edited on
You have:
 
std::cout<<jacob->id;
where jacob is nullptr.
but i have alocated jacob memory in heap with somefunction
Last edited on
No you haven't, that doesn't change the value of jacob.
Topic archived. No new replies allowed.