c++ a heap has been corrupted error when running the program

I have not reviewed the whole code, but there are tools available that can help you in this situation that track the usage of memory and indicate if something went wrong. One example is valgrind that is at least available for Linux environments. Anyway, this tool allowed me to find at least one bug in your code as follows.

  1. Compile with debug information. If you are using gcc, use the -g command line flag e.g. g++ foo.cpp -g -o foo -std=gnu++11
  2. Run with valgrind valgrind ./foo
  3. Look at the output ==6423== Memcheck, a memory error detector ==6423== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al. ==6423== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info ==6423== Command: ./foo ==6423== please enter your team name sdfads please enter the number of the max players on your team 3 please enter the name of the player efwf please enter the num of the shirt 5 please enter the name of the player dsfdsa please enter the num of the shirt 3 Team name: sdfads Max Number of players in team: 3 Current number of players in team: 0 Team Players: Player name: efwf, Player shirt: 5 Player name: dsfdsa, Player shirt: 3 ==6423== Invalid write of size 8 ==6423== at 0x4011FF: getAllPlayersStartWithA(team_t*) (foo.cpp:155) ==6423== by 0x400C08: main (foo.cpp:38) ==6423== Address 0x5ab6668 is 0 bytes after a block of size 8 alloc'd ==6423== at 0x4C2E80F: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==6423== by 0x4011CC: getAllPlayersStartWithA(team_t*) (foo.cpp:151) ==6423== by 0x400C08: main (foo.cpp:38) ==6423== ==6423== ==6423== HEAP SUMMARY: ==6423== in use at exit: 72,719 bytes in 3 blocks ==6423== total heap usage: 8 allocs, 5 frees, 74,829 bytes allocated ==6423== ==6423== LEAK SUMMARY: ==6423== definitely lost: 15 bytes in 2 blocks ==6423== indirectly lost: 0 bytes in 0 blocks ==6423== possibly lost: 0 bytes in 0 blocks ==6423== still reachable: 72,704 bytes in 1 blocks ==6423== suppressed: 0 bytes in 0 blocks ==6423== Rerun with --leak-check=full to see details of leaked memory ==6423== ==6423== For counts of detected and suppressed errors, rerun with: -v ==6423== ERROR SUMMARY: 2 errors from 1 contexts (suppressed: 0 from 0)
  4. Apparently you have a problem in line 155 according to this output ==6423== Invalid write of size 8 ==6423== at 0x4011FF: getAllPlayersStartWithA(team_t*) (foo.cpp:155) And if we look closer, we see the following: player_t **p = new player_t*[sum + 1]; for (int i = 0; i < team->maxOfPlayers; i++) { p[i] = NULL; } You create an array of size sum+1, but iterate over it up to team->maxOfPlayers that might or might not be the same. This means you write to some memory outside of the array that you want to modify and therefore you write somewhere in the heap where you should not (leading to a heap corruption).

This is at least one problem. Repeat 1.-4. until valgrind has nothing else to complain about.

Leave a Comment