C语言malloc(0)问题
来源:互联网 发布:网页代码优化 编辑:程序博客网 时间:2024/05/17 11:03
学C语言的时候遇到了malloc(0)的坑,原因是:
一些解释:
---------------------------------------------------------------------------------------------------------------------
MSDN上原话是“If size is 0, malloc allocates a zero-length item in the heap and returns a valid pointer to that item”
也就是说,如果申请内存大小为0,还是会返回一个有效指针
返回指针为NULL的条件是申请一定大小内存,内存空间不够的时候
----------------------------------------------------------------------------------------------------------------------------------------------
Allocates size bytes of uninitialized storage. If allocation succeeds, returns a pointer to the lowest (first) byte in the allocated memory block that is suitably aligned for any object type.
If size is zero, the behavior is implementation defined (null pointer may be returned, or some non-null pointer may be returned that may not be used to access storage)
----------------------------------------------------------------------------------------------------------------------------------------------
malloc() allocates size bytes and returns a pointer to the allocated
memory. The memory is not cleared. If size is 0, then malloc()
returns either NULL, or a unique pointer value that can later be suc-
cessfully passed to free().
-----------------------------------------------------------------------------------------------------------------------------------------------------------
If the size of the space requested is 0, the behaviour is implementation-dependent; the value returned will be either a null pointer or a unique pointer.
If size is 0, either a null pointer or a unique pointer that can be successfully passed to free() will be returned.
-----------------------------------------------------------------------------------------------------------------------------------------------------------
我自己的实际情况是:
在Dev-C++5.5.3中测试时,char * word=(char*)malloc(0); 有时候word是“”,有时候是一堆看不懂的乱码。
- C语言malloc(0)问题
- C语言 malloc(0)的问题
- c语言 malloc(0)的问题
- C语言中关于malloc(0)问题
- 关于c语言中的malloc问题
- C语言malloc用法
- C语言malloc\free
- C语言malloc/free
- C语言malloc用法
- c语言中的malloc
- c语言malloc函数
- c语言malloc分配
- C语言 malloc
- c语言malloc函数
- 关于c语言的malloc
- C语言中 malloc/free
- C语言之malloc函数
- c语言malloc和free
- 利用NFS在主机上挂载开发板文件
- Excel的导入与导出
- 【HDU】3715 Go Deeper 2-sat
- iOS: 学习笔记, 值与引用类型(译自: https://developer.apple.com/swift/blog/ Aug 15, 2014 Value and Reference Type
- 阿里实习转正面试
- C语言malloc(0)问题
- Qt-事件过滤器
- Java中的ClassLoader具体解说
- 正则表达式中的(?:exp)这样匹配表达式,有什么意义
- 【Hibernate】Hibernate对象标识符生成策略
- hdu2030汉字统计
- sicily 1172之水题
- php设置session过期
- poj 2328 Guessing Game