2017-06-02 128 views
-2
#include <stdlib.h> 
#include <stdio.h> 
int main() { 
    char **last_names; 
    // last_names has been assigned a char * array of length 4. Each element of the array has 
    // been assigned a char array of length 20. 
    // 
    // All of these memory has been allocated on the heap. 
    // Free all of the allocated memory (hint: 5 total arrays). 


    return 0; 
} 

我知道free()方法,這是我的方法;免費的免費動態內存()

free(*last_names); 
free(last_names); 

但事實並非如此。任何幫助將不勝感激

+2

而不是描述你的配置,在代碼中顯示它。 –

+1

請添加代碼(1)分配'last_names',並(2)將單個元素分配給'last_names' – dasblinkenlight

回答

0

從你的代碼的描述猜測,您的內存分配將是:

last_names = malloc(4 * sizeof(char*)); 
for (int i = 0; i < 4; i++) 
    last_names[i] = malloc(20 * sizeof(char)); 

所以釋放應做如下:

for (int i = 0; i < 4; i++) 
    free(last_names[i]); 
free(last_names);