可用内存在C(无法修复的内存泄漏) [英] Free Memory in C (Can't fix memory leak)

查看:129
本文介绍了可用内存在C(无法修复的内存泄漏)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:我已经根据建议人们进行了,但我无法修复的内存泄漏改变了我的计划。另外,我需要释放他们不使用的argc,所以我需要以某种方式保持数组长度的轨道,所以我打上最后一个元素为空。

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.

目前我正在写一个C程序的复制命令行参数为一个动态分配的数组。我的code如下:

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.

感谢!

推荐答案

您最后一行免费(阵列)不释放你原来的malloc,因为你已经增加阵列为你释放它的内容。

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.

您在分配阵列ARGC + 1的元素,当你只需要ARGC。

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

*(的argv + I)相同的argv [I]

这篇关于可用内存在C(无法修复的内存泄漏)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆