代码按预期工作,但它永远不会释放分配的内存malloc().
我试图在任何可以的地方释放记忆,但无论我在哪里,它都会破坏程序.具体来说,我得到了"双重免费或损坏错误".这更像是关于什么free()和malloc()实际做的问题?免费的所有问题主要在于:
int main(int argc, char *argv[]){
if(argc!=2){
exit(1);
}
printf("CSA WC version 1.0\n\n");
int length = strlen(argv[argc-1]);
char file_to_open[length];
strcpy(file_to_open, argv[argc-1]);
//printf("filename:%s\n",file_to_open);
//create counters for output
int count_number_of_lines = 0;
int count_number_of_words = 0;
int count_number_of_characters = 0;
//create int size of default array size
int current_array_size = pre_read(file_to_open);
//printf("number of lines: %i\n",current_array_size);
//create string array of default size
char *strings_array[current_array_size];
//create a pointer to catch incoming strings
char *incoming_string=NULL;
int done=0;
while(done==0){
incoming_string=get_line_from_file(file_to_open, count_number_of_lines);
if(incoming_string!=NULL){
incoming_string=csestrcpy2(incoming_string);
//printf("incoming line: %s\n",incoming_string);
strings_array[count_number_of_lines]=(char*)malloc(strlen(incoming_string+1));
strings_array[count_number_of_lines]=csestrcpy2(incoming_string);
//printf("added to array:%s\n",strings_array[count_number_of_lines]);
count_number_of_lines++;
count_number_of_characters=(count_number_of_characters+(strlen(incoming_string)-1));
}
else{
done=1;
}
}
//all data is stored in a properly sized array
//count all words in array
int count=0;
int word_count=0;
char *readline;
while(count<current_array_size){
readline = csestrcpy2(strings_array[count]);
printf("line being checked: %s", readline);
int i=0;
int j=1;
while( j< strlen(readline)+1 ){
if(strcmp(readline,"\n")!=0){
if( (readline[i] == ' ') && (readline[j] != ' ') ){
word_count++;
}
if( (readline[i] != ' ') && (readline[j] == '\n') ){
word_count++;
}
}
i++;
j++;
}
count++;
}
printf("current word count: %i", word_count);
return 0;
}
char* csestrcpy2(char* src){
int i = 0;
char *dest;
char t;
dest = (char*) malloc(MAX_LINE);
while( src[i] != '\0'){
dest[i] = src[i];
i++;
}
dest[i] = '\0';
//printf("length:%i\n",i);
free(dest);
return dest;
}
Run Code Online (Sandbox Code Playgroud)
Flo*_*ian 13
通常,您只需释放已动态保留的内存.这意味着如果你有这样的声明:
int *my_int_pointer;
my_int_pointer = malloc(sizeof(int));
Run Code Online (Sandbox Code Playgroud)
而不是需要释放malloc分配(保留)的内存.如果你不确定在哪里释放它,而不是在节目结束时免费使用免费;
free(my_int_pointer);
Run Code Online (Sandbox Code Playgroud)
在您的文件中,只要您读取的文件中有新行(在while(done==0)循环中),就会分配内存.因此,每次在if此循环之后,您必须释放变量使用的内存.
此外,您需要释放为readline变量分配的内存.但正如之前所指出的那样,你可能会有内存泄漏.
希望这可以帮助.
编辑:好的 - 我已经想知道这个csestrcpy功能了.让我们来看看这个功能:
char* csestrcpy2(char* src){
int i = 0;
char *dest;
char t;
dest = (char*) malloc(MAX_LINE); /*<<- This allocates memory that has to be freed*/
while( src[i] != '\0'){
dest[i] = src[i];
i++;
}
dest[i] = '\0';
//printf("length:%i\n",i);
free(dest); /* This frees the memory, but you return a pointer */
return dest; /* to this memory. this is invalid. */
}
Run Code Online (Sandbox Code Playgroud)
你可以自由的是该函数中的src指针.但请记住:在释放底层内存后,指针无法保存信息!它只是指向内存中不应该写或读的地方.
此外,只要没有'\ 0',函数就会复制字符串.如果没有终结器会怎么样?该功能继续从一些不应该的内存地址复制!
你不应该使用该功能;)
free()每次成功通话都需要拨打电话malloc().
这并不一定意味着您需要在代码中使用相同数量的malloc()和free()调用; 这意味着对于malloc()程序运行时执行的每个调用,你应该调用free()它,传递你得到的指针值malloc(). malloc()分配内存; free()告诉系统你已完成分配的内存.
(free()ing当你的程序终止时,你几乎可以肯定地没有分配内存,因为它将被操作系统回收,但是作为风格和良好实践的问题,你仍然应该将malloc()s与free()s 匹配.)
我无视calloc()并realloc()打电话.