Am 19.05.21 um 00:09 schrieb Chris M. Thomasson:
You are basically accessing memory that was "freed"[*] before. That is
totally undefined, of course, but there is no guarantee that it
segfaults. The CPU can not check every small allocation on the stack or
heap for correctness. Instead, the memory is structured into pages of
typically 4kB in size. The CPU only checks that the page was allocated
for the current process, so as long as there is any other valid object
in this page, there is no segfault. This also explains why programs with
memory errors often crash in a totally different location. If a pointer
gets mangled, then it segfaults at the point where the pointer is used
and suddenly points into the woods.
You can use a memory debugger to detect these problems, like valgrind on
Linux (one of the best) or the flag -fsanitize=address with clang. Then,
every object is allocated on its own page, making the program very
bloated and slow, but the program will then segfault immediately at the
point where the deleted object is accessed.
[*] not on the heap, so not "malloc/free-type freed", but "stack-freed"
Best regards,
Christian