C Debugging Note

==Updating==dom

Memory leak

Things that could result in a memory leak :debug

  • Assigning NULL to a dynamically allocated memory block. Will not be freed later.
char ** arg = calloc(1, sizeof(char *));
    arg[0] = calloc(10, sizeof(char));
    arg[0] = NULL;
    free(arg); // MEMORY LEAK!
  • Rewriting to a freed memory block.code

    In some weird cases, the programme will run smoothly for several steps (writing) and then throw a segmentation fault error.get

To save yourself some time from debugging memory leak issues, here are the things you can do:it

  • Always draw a diagram for the changes you made to the dynamically allocated blocksio

  • Attach debugging messages to each operation on the dynamically allocated blocks. Use debug flags.import


An ultimate wisdom:bug

The most important thing is to get it run with no error whatsoever, but not to do research on the cause of the problem.im

Therefore, if you have stuck for some time, re-write the piece of code -- rewriting always solves the problem.call

相關文章
相關標籤/搜索