Malloc inside a function call appears to be getting freed on return?

前端 未结 5 706
囚心锁ツ
囚心锁ツ 2020-12-07 16:05

I think I\'ve got it down to the most basic case:

int main(int argc, char ** argv) {
  int * arr;

  foo(arr);
  printf(\"car[3]=%d\\n\",arr[3]);
  free (arr         


        
5条回答
  •  北荒
    北荒 (楼主)
    2020-12-07 16:47

    You cannot change the value of your argument (arr) if it's not passed in by reference (&). In general, you would want to return the pointer, so your method should be:

    arr=foo();

    It's bad juju to try to reassign arguments; I don't recommend the (&) solution.

提交回复
热议问题