Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the functions of malloc and new and realloc respectively

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

In this issue, Xiaobian will bring you about what malloc and new and realloc functions are respectively. The article is rich in content and analyzed and described from a professional perspective. After reading this article, I hope you can gain something.

1. Malloc function

void *malloc(size_t size); Description: malloc requests the system to allocate memory space of the specified size bytes. The return type is void* type.

There are at least two differences between malloc and new:

new Returns a pointer to the specified type and automatically calculates the required size. For example: int *p; p = new int; //return type int *(integer pointer), allocation size sizeof(int);

Or: int* parr; parr = new int [100]; //Return type int* type (integer pointer), allocation size sizeof(int) * 100;

Malloc, on the other hand, must be counted by us and forcibly converted to a pointer of the actual type after returning.

int* p; p = (int *) malloc (sizeof(int)*128);//Allocate 128 integer memory locations (which can be replaced as needed) and store the first address of these 128 consecutive integer memory locations in the pointer variable p

double *pd=(double *) malloc (sizeof(double)*12);//Allocate 12 double memory locations and store the first address in pointer variable pd

Except for the allocation and final release methods, the pointer is obtained by malloc or new, and the other operations are consistent.

C language function realloc

prototype: extern void *realloc(void *mem_address, unsigned int newsize);

Syntax: pointer name =(data type *) realloc (pointer name to change memory size, new size).// The new size must be larger than the original size or it will cause data loss!

Header file: #include Some compilers need #include, in TC 2.0 you can use alloc.h header file

Function: first allocate space according to the size specified by newsize, copy the original data from beginning to end to the newly allocated memory area, then release the memory area referred to by the original mem_address, and return the first address of the newly allocated memory area. i. e., reallocate addresses of memory blocks.

Return value: If the reallocation is successful, it returns a pointer to the allocated memory, otherwise it returns NULL pointer.

Note: The data in the original memory remains unchanged. When memory is no longer in use, the free() function should be used to free blocks of memory.

The above is what the malloc and new and realloc functions shared by Xiaobian are respectively. If there are similar doubts, please refer to the above analysis for understanding. If you want to know more about it, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report