How to free memory from char array in C

前端 未结 4 928
生来不讨喜
生来不讨喜 2021-02-01 15:15

I created a char array like so:

char arr[3] = \"bo\";

How do I free the memory associated with array I named \"arr\"?

4条回答
  •  不思量自难忘°
    2021-02-01 15:32

    Local variables are automatically freed when the function ends, you don't need to free them by yourself. You only free dynamically allocated memory (e.g using malloc) as it's allocated on the heap:

    char *arr = malloc(3 * sizeof(char));
    strcpy(arr, "bo");
    // ...
    free(arr);
    

    More about dynamic memory allocation: http://en.wikipedia.org/wiki/C_dynamic_memory_allocation

提交回复
热议问题