0

I was testing valgrind and came up to this code, which only mallocs structure.

#include "env.h"

int     main(int argc, char **argv, char **envp)
{
    t_env   *env;
    int     i;

    env = malloc(sizeof(t_env));

    return (0);
}
typedef struct  s_env
{
    char        **global_env;
    int         global_size;
}               t_env;

I can't figure out why this is considered definitely lost. Shouldn't it be considered still reachable? Would be glad to teach me!


Sorry , I found out this behavior, that it's right to call definitely lost reading this article Why valgrind report my memory as "definitely lost"?

when it comes to global variable, and not freeing, then it's considered still reachable. But when it's local variable, it's considered definitely lost.

mchun
  • 79
  • 1
  • 8
  • Once the method returns, it's not reachable anymore and it's a valid warning. Calling `free(env)` is polite to the memory manager. – Joachim Isaksson Aug 29 '21 at 13:45
  • @Joachim Isaksson ,thank you. I thought it should be considered Still reachable because `main(){}` had malloced memory. Must be confused with not free'd global variable. – mchun Aug 29 '21 at 13:56

0 Answers0