用malloc或calloc申请内存之后,
用malloc或calloc申请内存之后,应该立即检查指针值是否为NULL。防止使用指针值为NULL的内存。
请问用这两个函数申请内存的时候是什么工作原理,为什么有可能申请到值为NULL的内存呢?
[解决办法]
申请内存的原理就要看操作系统原理了。“有可能申请到值为NULL的内存”,意思应该是说当申请内存失败后,会返回一个NULL的指针。
[解决办法]
malloc返回0说明分配失败,你需要根据这个情况作出合理的行为
[解决办法]
[解决办法]
malloc returns a void pointer to the allocated space, or NULL if there is insufficient memory available.
calloc returns a pointer to the allocated space. The storage space pointed to by the return value is guaranteed to be suitably aligned for storage of any type of object.