Does memory allocated in a function still stay allocated after the function returns?

后端 未结 7 2030
鱼传尺愫
鱼传尺愫 2021-02-04 06:57

For the code below: (1) \"main\" calls a function \"f1\". (2) function \"f1\" does some number crunching; creates an array of \"char\" with mal

7条回答
  •  旧巷少年郎
    2021-02-04 07:09

    1. Yes, it's still in the heap. However, you are confusing about the concept of process. Unless you create another process (using fork on *nix), it's still the same process.

    2. It's a good habit to free the memory when it's not used. But if the program terminates normally, the allocated memory is freed by the system.

    3. Like this:

      int main () {
          char *fData = f1 (...);
          //...
          free(fData);
          //...
      }
      

提交回复
热议问题