Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the meaning of realloc function in C language

Shulou Source: shulou.com Published: 2022-06-02 22:53:53 10月03日 Update

This article mainly explains "what is the meaning of realloc function in c language". The content of the explanation in this article is simple and clear, and it is easy to learn and understand. please follow the editor's train of thought to study and learn "what is the meaning of realloc function in c language"?

1. Judge whether the current pointer has enough continuous space. If enough, expand the address that ptr points to and return.

If not, how to allocate space according to the size specified by the size, copy the original data into the newly allocated memory, and then release the area pointed to by the original ptr.

2. After using memory, you should use the free () function to release, but the original pointer is automatically released without the need to use free.

Example

# include # include int main () {char * str; / * initial memory allocation * / str = (char *) malloc (15); strcpy (str, "Hello World!"); printf ("String =% s\ n", str); / * reallocate memory * / str = (char *) realloc (str, 25); strcat (str, ", C"); printf ("String =% s\ n", str); free (str); system ("pause"); return 0 Thank you for reading, the above is the content of "what is the meaning of realloc function in c language". After the study of this article, I believe you have a deeper understanding of the meaning of realloc function in c language, and the specific use needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Function meaning language memory allocation learning original content pointing pointer space insufficient region address size instance that is idea situation data Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Shulou Information Docker Microsoft Linux