求mallco 和 free 函数原型
如题!
[解决办法]
函数malloc 的原型如下:
1.void * malloc(size_t size); 用malloc 申请一块长度为length 的整数类型的内存,程序如下:
1.int *p = (int *) malloc(sizeof(int) * length); 我们应当把注意力集中在两个要素上:“类型转换”和“sizeof”。
1、malloc 返回值的类型是void *,所以在调用malloc 时要显式地进行类型转换,将void * 转换成所需要的指针类型。
2、malloc 函数本身并不识别要申请的内存是什么类型,它只关心内存的总字节数。
函数free 的原型如下:
1.void free( void * memblock ); 为什么free 函数不象malloc 函数那样复杂呢?这是因为指针p 的类型以及它所指的内存的容量事先都是知道的,语句free(p)能正确地释放内存。如果p 是NULL 指针,那么free对p 无论操作多少次都不会出问题。如果p 不是NULL 指针,那么free 对p连续操作两次就会导致程序运行错误。
[解决办法]
你想看它们的具体定义?这个东西和操作系统相关的,不同系统下方式完全不同。
而且,这个对编程的人来说没什么实际意义。它们如何从系统那里拿到空间和归还空间的过程你何必了解?只要会使用它们就行了。如果一切都要弄明白,那就没完没了了,你得追究到哪个层次呢?一直追下去就得去学原子理论了,甚至还不肯罢休,那得去和核物理学家们讨论夸克什么的了。
[解决办法]
拓展阅读
malloc free与new delete的区别和联系 .
我的博客
http://blog.csdn.net/kuzuozhou/article/details/7290989
[解决办法]
malloc:
Allocates memory blocks.
void *malloc(
size_t size
);
Parameters
size
Bytes to allocate.
Return Value
malloc returns a void pointer to the allocated space or NULL if there is insufficient memory available. To return a pointer to a type other than void, use a type cast on the return value. The storage space pointed to by the return value is guaranteed to be suitably aligned for storage of any type of object. If size is 0, malloc allocates a zero-length item in the heap and returns a valid pointer to that item. Always check the return from malloc, even if the amount of memory requested is small.
Remarks
The malloc function allocates a memory block of at least size bytes. The block may be larger than size bytes because of space required for alignment and maintenance information.
In Visual C++ 2005, malloc sets errno to ENOMEM if a memory allocation fails or if the amount of memory requested exceeds _HEAP_MAXREQ. For information on this and other error codes, see errno, _doserrno, _sys_errlist, and _sys_nerr.
The startup code uses malloc to allocate storage for the _environ, envp, and argv variables. The following functions and their wide-character counterparts also call malloc:
free:
Deallocates or frees a memory block.
void free(
void *memblock
);
Parameters
memblock
Previously allocated memory block to be freed.
Remarks
The free function deallocates a memory block (memblock) that was previously allocated by a call to calloc, malloc, or realloc. The number of freed bytes is equivalent to the number of bytes requested when the block was allocated (or reallocated, in the case of realloc). If memblock is NULL, the pointer is ignored and free immediately returns. Attempting to free an invalid pointer (a pointer to a memory block that was not allocated by calloc, malloc, or realloc) may affect subsequent allocation requests and cause errors.
In Visual C++ 2005, if an error occurs in freeing the memory, errno is set with information from the operating system on the nature of the failure. For more information, see errno, _doserrno, _sys_errlist, and _sys_nerr.
After a memory block has been freed, _heapmin minimizes the amount of free memory on the heap by coalescing the unused regions and releasing them back to the operating system. Freed memory that is not released to the operating system is restored to the free pool and is available for allocation again.
When the application is linked with a debug version of the C run-time libraries, free resolves to _free_dbg. For more information about how the heap is managed during the debugging process, see The CRT Debug Heap.
free is marked __declspec(noalias), meaning that the function is guaranteed not to modify global variables. For more information, see noalias.
To free memory allocated with _malloca, use _freea.
源自vs2008 msdn