📄 calloc.cpp
字号:
// this file is slightly modified version from MS VC 6.0 CRT Sources.
// .\libmad\layer3.c needs calloc() function so I implement it here
/***
*void *calloc(size_t num, size_t size) - allocate storage for an array from
* the heap
*
*Purpose:
* Allocate a block of memory from heap big enough for an array of num
* elements of size bytes each, initialize all bytes in the block to 0
* and return a pointer to it.
*
*Entry:
* size_t num - number of elements in the array
* size_t size - size of each element
*
*Exit:
* Success: void pointer to allocated block block
* Failure: NULL
*
*Uses:
*
*Exceptions:
*
*******************************************************************************/
extern "C" {
#pragma warning ( push, 3 )
#include <ntddk.h>
#pragma warning ( pop )
}
#pragma warning ( disable: 4514 ) // unreferenced inline function has been removed
#pragma warning ( disable: 4273 ) // inconsistent dll linkage. dllexport assumed.
#include <malloc.h>
void * __cdecl calloc (
size_t num,
size_t size
)
{
void *retp;
size_t *startptr;
size_t *lastptr;
/* try to malloc the requested space
*/
retp = malloc(size *= num);
/* if malloc() succeeded, initialize the allocated space to zeros.
* note the assumptions that the size of the allocation block is an
* integral number of sizeof(size_t) bytes and that (size_t)0 is
* sizeof(size_t) bytes of 0.
*/
if ( retp != NULL ) {
startptr = (size_t *)retp;
lastptr = startptr + ((size + sizeof(size_t) - 1) /
sizeof(size_t));
while ( startptr < lastptr )
*(startptr++) = 0;
}
return retp;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -