Valgrind legitimate “possibly lost” bytes example

后端 未结 3 1566
情书的邮戳
情书的邮戳 2021-01-11 19:14

I saw that valgrind classifies memory leaks into:

  • definitely lost
  • indirectly lost
  • possibly lost
  • still reachable
3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-11 20:01

    char *p = malloc(100);
    if (p != 0)
    {
        p += 50;
        /* at this point, no pointer points to the start of the allocated memory */
        /* however, it is still accessible */
        for (int i = -50; i != 50; i++)
            p[i] = 1;
        free (p - 50);
    }
    

提交回复
热议问题