со страницы руководства:
Если ptr не имеет значения NULL, он должен
возвращаться функцией более ранний вызов malloc(), calloc() или realloc().
Почему это нужно?
скомпилируйте это с gcc (Ubuntu 9.4.0-1ubuntu1~20.04.2) 9.4.0 без опций(флагов)
этот пример
Код: Выделить всё
#include
int main () {
int *p = malloc(0);
p = realloc(p, 0);
return 0;
}
Код: Выделить всё
valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes ./test
==185872== Memcheck, a memory error detector
==185872== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==185872== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==185872== Command: ./test
==185872==
==185872==
==185872== HEAP SUMMARY:
==185872== in use at exit: 0 bytes in 0 blocks
==185872== total heap usage: 1 allocs, 1 frees, 0 bytes allocated
==185872==
==185872== All heap blocks were freed -- no leaks are possible
==185872==
==185872== For lists of detected and suppressed errors, rerun with: -s
==185872== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from
Код: Выделить всё
#include
int main () {
int *p = NULL;
p = realloc(p, 0);
return 0;
}
Код: Выделить всё
valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes ./test
==186749== Memcheck, a memory error detector
==186749== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==186749== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==186749== Command: ./test
==186749==
==186749==
==186749== HEAP SUMMARY:
==186749== in use at exit: 0 bytes in 1 blocks
==186749== total heap usage: 1 allocs, 0 frees, 0 bytes allocated
==186749==
==186749== 0 bytes in 1 blocks are definitely lost in loss record 1 of 1
==186749== at 0x483B723: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==186749== by 0x483E017: realloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==186749== by 0x10916D: main (test1.c:5)
==186749==
==186749== LEAK SUMMARY:
==186749== definitely lost: 0 bytes in 1 blocks
==186749== indirectly lost: 0 bytes in 0 blocks
==186749== possibly lost: 0 bytes in 0 blocks
==186749== still reachable: 0 bytes in 0 blocks
==186749== suppressed: 0 bytes in 0 blocks
==186749==
==186749== For lists of detected and suppressed errors, rerun with: -s
==186749== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
Подробнее здесь: https://stackoverflow.com/questions/793 ... s-argument