C語言中動態記憶體管理函數

來源:互聯網
上載者:User
文章目錄
  • Parameters
  • Return Value
  • Parameters
  • Return Value
  • Parameters
  • Return Value
  • Parameters

記憶體配置

mallocfunction<cstdlib>

void * malloc ( size_t size );

Allocate memory block

Allocates a block of  size bytes of memory, returning a pointer to the beginning of the block.
The content of the newly allocated block of memory is not initialized, remaining with indeterminate values.
Parameters
size
Size of the memory block, in bytes.
Return Value

On success, a pointer to the memory block allocated by the function.
The type of this pointer is always void*, which can be cast to the desired type of data pointer in order to be dereferenceable.
If the function failed to allocate the requested block of memory, a null pointer is returned.

reallocfunction<cstdlib>

void * realloc ( void * ptr, size_t size );

Reallocate memory block

The size of the memory block pointed to by the  ptr parameter is changed to the  size bytes, expanding or reducing the amount of memory available in the block.

The function may move the memory block to a new location, in which case the new location is returned. The content of the memory block is preserved up to the lesser of the new and old sizes, even if the block is moved. If the new size is larger, the value of the newly allocated portion is indeterminate.

In case that ptr is NULL, the function behaves exactly as malloc, assigning a new block of size bytes and returning a pointer to the beginning of it.

In case that the size is 0, the memory previously allocated in ptr is deallocated as if a call to free was made, and a NULL pointer is returned.
Parameters

ptr
Pointer to a memory block previously allocated with malloc, calloc or realloc to be reallocated.
If this is NULL, a new block is allocated and a pointer to it is returned by the function.
size
New size for the memory block, in bytes.
If it is 0 and  ptr points to an existing block of memory, the memory block pointed by  ptr is deallocated and a NULL pointer is returned.
Return Value

A pointer to the reallocated memory block, which may be either the same as the ptr argument or a new location.
The type of this pointer is void*, which can be cast to the desired type of data pointer in order to be dereferenceable.
If the function failed to allocate the requested block of memory, a NULL pointer is returned. 

callocfunction<cstdlib>

void * calloc ( size_t num, size_t size );

Allocate space for array in memory

Allocates a block of memory for an array of  num elements, each of them  size bytes long, and initializes all its bits to zero.

The effective result is the allocation of an zero-initialized memory block of (num * size) bytes.
Parameters

num
Number of elements to be allocated.
size
Size of elements.
Return Value

A pointer to the memory block allocated by the function.
The type of this pointer is always void*, which can be cast to the desired type of data pointer in order to be dereferenceable.
If the function failed to allocate the requested block of memory, a NULL pointer is returned.

記憶體釋放

freefunction<cstdlib>

void free ( void * ptr );

Deallocate space in memory

A block of memory previously allocated using a call to malloc, calloc or realloc is deallocated, making it available again for further allocations.

Notice that this function leaves the value of ptr unchanged, hence it still points to the same (now invalid) location, and not to the null pointer.
Parameters

ptr
Pointer to a memory block previously allocated with malloc, calloc or realloc to be deallocated.
If a null pointer is passed as argument, no action occurs.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.