Scope of new memory in C++

前端 未结 3 1765
暗喜
暗喜 2021-01-14 11:08

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];
          


        
3条回答
  •  星月不相逢
    2021-01-14 11:52

    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)

提交回复
热议问题