Programming Notes

  1. Malloc
    Malloc is needed in C because sometimes you want memory to be persistent for the life of a program, even when leaving and entering functions and so on. Malloc sets memory aside from the heap and allows it to be persistent until either freed or the program exits. Linked lists need this because we want the information in a list to exist while the main program exists. Arrays allocate space for themselves when they are made, but since they could be local variables then they are more at risk of being lost than if memory is explicitly allocated.

  2. Set the pointer to NULL after free
    After freeing anything with free(), set its pointer to NULL.
    This may solve some difficult-to-find bugs */

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容