heap corruption detected | C++

“Heap corruption” generally means you wrote into unallocated memory, damaging the data structures used to make the memory allocator work.

There may be more problems, but the first one I see is on this line:

strcpy(buffer, n);

This will write strlen(n) + 1 bytes to buffer, but buffer is only strlen(n) bytes long (the extra byte is the terminating \0.) Writing that extra byte results in undefined behavior, and may well corrupt the heap.

Leave a Comment