我無法在我的C程序中釋放內存,當我運行該程序時,我得到12個分配和3個釋放或24個分配和8個釋放。我釋放了我正在使用的兩個變量,但我仍然在泄漏。我在這裏做錯了什麼? 注意我不允許使用libc的功能我不得不重新編碼它們,這是一個學校作業。無法釋放內存C
我memalloc.c功能
void *ft_memalloc(size_t size)
{
void *temp;
//needs to be freed from the caller
temp = (void *)malloc(sizeof(*temp) * (size + 1));
if (temp)
ft_bzero(temp, size + 1);
return (temp);
}
我strsub.c
char *ft_strsub(char const *s, unsigned int start, size_t len)
{
char *sub;
//sub needs to be freed from the caller
sub = ft_memalloc(len + 1);
if (sub)
ft_memcpy(sub, s + start, len);
return (sub);
}
我strsplit.c
static int count_words(char *s, char c)
{
int words;
words = 0;
while (*s && *s == c)
++s;
if (*s)
words = 1;
while (*s)
{
if (*s == c && s[1] && s[1] != c)
++words;
++s;
}
return (words);
}
char **ft_strsplit(char const *s, char c)
{
int words;
char *start;
char **result;
words = count_words((char *)s, c);
if (!s || !c)
return (NULL);
result = (char **)malloc(sizeof(char *) * (count_words((char *)s, c) + 1));
start = (char *)s;
while (*s)
{
if (*s == c)
{
if (start != s)
*(result++) = ft_strsub(start, 0, s - start);
start = (char *)s + 1;
}
++s;
}
if (start != s)
*(result++) = ft_strsub(start, 0, s - start);
*result = NULL;
return (result - words);
}
get_line功能
int ft_get_line_helper(char *text, int buf_size, char **line, const int fd)
{
int position;
int c;
position = 0;
while (1)
{
c = ft_getchar(fd);
if (c == 0 || c == '\n')
{
text[position] = '\0';
*line = text;
return (1);
}
else
text[position] = c;
position++;
if (position >= buf_size)
{
buf_size += BUFF_SIZE;
text = ft_realloc(text, buf_size);
if (!text)
return (-1);
}
}
return (1);
}
int get_next_line(const int fd, char **line)
{
char *text;
int buf_size;
buf_size = BUFF_SIZE;
text = (char *)malloc(sizeof(char) * buf_size);
if (fd < 0 || !text || !line)
return (-1);
return (ft_get_line_helper(text, buf_size, line, fd));
}
的main.c
void prompt(char **commands)
{
ft_putstr(GRN);
ft_putstr("$> ");
ft_putstr(RESET);
get_next_line(0, commands);
}
int main(int ac, char const **av, char **envp)
{
char *get_line;
char **user_comm;
//voided for now will use them later
(void)ac;
(void)av;
(void)envp;
while (42) {
prompt(&get_line);
{
user_comm = ft_strsplit(get_line, ' ');
if (ft_strequ(user_comm[0], "exit"))
exit(0);
ft_putstr(user_comm[0]);
//freeing my variables here
free(user_comm);
free(get_line);
}
}
return 0;
}
Valgrind的結果
==7342==
==7342== HEAP SUMMARY:
==7342== in use at exit: 80 bytes in 8 blocks
==7342== total heap usage: 18 allocs, 10 frees, 320 bytes allocated
==7342==
==7342== 26 bytes in 5 blocks are definitely lost in loss record 3 of 4
==7342== at 0x4C2DB9D: malloc (vg_replace_malloc.c:299)
==7342== by 0x400CA1: ft_memalloc (in /home/julekgwa/minishell/minishell)
==7342== by 0x400B7E: ft_strsub (in /home/julekgwa/minishell/minishell)
==7342== by 0x400900: ft_strsplit (in /home/julekgwa/minishell/minishell)
==7342== by 0x4006E3: main (main.c:23)
==7342==
==7342== LEAK SUMMARY:
==7342== definitely lost: 26 bytes in 5 blocks
==7342== indirectly lost: 0 bytes in 0 blocks
==7342== possibly lost: 0 bytes in 0 blocks
==7342== still reachable: 54 bytes in 3 blocks
==7342== suppressed: 0 bytes in 0 blocks
==7342== Reachable blocks (those to which a pointer was found) are not shown.
==7342== To see them, rerun with: --leak-check=full --show-leak- kinds=all
==7342==
==7342== For counts of detected and suppressed errors, rerun with: -v
==7342== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
注意'sizeof(void)'無效。 – BLUEPIXY