Я получаю ошибку valgrind при проверке моей программы на утечки памяти.
Ошибка происходит где-то в моей функции cutString при выделении / перераспределении памяти, но я не уверен, что делаю неправильно.
Я неправильно распределяю память?
Вот вывод valgrind:
$ valgrind --leak-check=full --track-origins=yes ./cutstring
==7017== Memcheck, a memory error detector
==7017== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==7017== Using Valgrind-3.14.0 and LibVEX; rerun with -h for copyright info
==7017== Command: ./cutstring
==7017==
Hell
==7017==
==7017== HEAP SUMMARY:
==7017== in use at exit: 5 bytes in 1 blocks
==7017== total heap usage: 3 allocs, 2 frees, 1,042 bytes allocated
==7017==
==7017== 5 bytes in 1 blocks are definitely lost in loss record 1 of 1
==7017== at 0x4839D7B: realloc (vg_replace_malloc.c:826)
==7017== by 0x109205: cutString (in cutstring)
==7017== by 0x109228: main (in cutstring)
==7017==
==7017== LEAK SUMMARY:
==7017== definitely lost: 5 bytes in 1 blocks
==7017== indirectly lost: 0 bytes in 0 blocks
==7017== possibly lost: 0 bytes in 0 blocks
==7017== still reachable: 0 bytes in 0 blocks
==7017== suppressed: 0 bytes in 0 blocks
==7017==
==7017== For counts of detected and suppressed errors, rerun with: -v
==7017== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
Это мой код:
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
char *cutString(char *str, char del)
{
char *new_string = (char*) malloc(strlen(str) * sizeof(char) + 1);
int i = 0;
while (str[i] != del)
{
new_string[i] = str[i];
i++;
}
new_string[i] = '\0';
new_string = (char*) realloc(new_string, strlen(new_string) + 1);
return new_string;
free(new_string);
}
int main()
{
printf("%s\n", cutString("Hello World!", 'o'));
return 0;
}
Я предполагаю, что я использовал realloc неправильно, но я не могу понять, почему.
Буду признателен за помощь, спасибо!