开发者

Free Memory in C (Can't fix memory leak)

开发者 https://www.devze.com 2023-04-13 07:43 出处:网络
EDIT: I have changed my program according to suggestions people have made but I am unable to fix memory leaks. Also, I need to free them without using argc, so i need to somehow keep track of the arra

EDIT: I have changed my program according to suggestions people have made but I am unable to fix memory leaks. Also, I need to free them without using argc, so i need to somehow keep track of the array length so I marked the last element as null.

Currently I'm writing a C program that copies the command line arguments into a dynamically allocated array. My code looks like:

char **array;                                                                                                                                                                                     
int j;                                                                                                                                                                                        

array = malloc(sizeof(char*) * (argc + 1));                                                                                                                                                       
int i;                                                                                                                                                                                            
int index = 0;                                                                                                                                                                                    

for(i = 0; i < (argc); i++){                                                                                                                                                                      
    int length = strlen(*(argv + i));                                                                                                                                                         开发者_如何转开发    
    array[i] = malloc((length + 1) * sizeof(char));                                                                                                                                                                                                                                                                                                                                    
        // Cycle through all the chars and copy them in one by one                                                                                                                                
    for(j = 0; j <= length; j++){                                                                                                                                                                 
        array[i][j] = toupper(argv[i][j]);                                                                                                                                                        
    }                                                                                                                                                                                             
}      
array[i + 1] = NULL;                                                                                                                                                                                           

return array;      

Later, I try to free the memory:

char** array_copy = array;
while(*array_copy != NULL){
    free(*array_copy++);
}
free(*array_copy) // free the null at the end
free(array); 

However, I still get memory leaks. I'm not quite sure what I'm doing wrong. If anyone could give me tips that would be great.

Thank!


Your last line free(array) isn't freeing your original malloc, because you've incremented array as you are freeing its contents.

Also (as others point out):

  • your loop to free the array contents is checking for non-zero, but you don't ensure that the elements are zero to begin with.

  • You allocate argc+1 elements in array, when you only need argc.

  • *(argv + i) is the same as argv[i].


array has been completely ++'d off of the beginning. The free(array) is corrupting your heap, not freeing.

add

char ** array_iter = array;

and then change the loop to

while(*array_iter){
    free(*array_iter++);
}
free(array);


By using the post-increment, you're setting your pointer equal to something other than what it was originally. You're no longer freeing the right memory.


Your while loop isn't working. First only allocate argc times space for char*. Then allocate strlen + 1 for each string (null terminator) use strcpy to copy the strings and uppercase them afterwards (less error prone). Then have a free like this:

for(int i = 0; i < argc; ++i) free(array[i]);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号