When I try to do the following I get an error saying I’m trying to read or write to protected memory.
void func1(int * ptr) {
int *ptr_b = new int[5];
ptr = ptr_b;
}
void main() {
int *ptr_a;
func1(ptr_a);
delete [] ptr_a;
}
Is this legal?
No. You’re making a common beginner mistake. You’re not remembering that pointers are just variables that are passed by value unless you ask for a reference or pointer to them. Change the signature of your function to
void func1(int *& ptr)