C language from entry to proficiency on the 19th day (malloc and realloc function for memory allocation)

memory allocation

malloc function

When we need to store multiple data, we will think of using data. When we know how much data is, we can define the length of the data by ourselves, but when we want to add data, the length of the array is fixed at this time. It cannot be added anymore. At this point, we can first define a pointer variable according to the data type that needs to be stored, and then use the malloc function to allocate space according to actual needs.

The malloc function is in stdlib.hthe library, we need to include this header file when using it.

#include <stdlib.h>
void *malloc(sieze_t size)

The malloc function applies for a memory space of size bytes from the system, and returns a pointer (by default, a void *pointer of type ()), which points to the first address of the allocated memory space, and the requested memory space is in **"heap" **superior.

Note: The space on the heap needs to be manually applied and released, otherwise it will cause memory leaks. The space on the stack is automatically allocated and reclaimed automatically.

Examples are as follows:

int *p;
// 假如我们需要存储10个int类型的数据
p = (int *)malloc(10 *sizeof(int))

The space allocated by malloc is in bytes. And in principle, the data type returned by the malloc function is ( void *) type, because the data type to be stored is integer, so here we will force it to ( int *) type.

code show as below:

#include<stdlib.h>
int main(){
    
    
    // 定义一个野指针
    int *p;
    // printf("%p\n",p);

    // 这里给指针分配10个整型空间
    // 内存在堆上进行申请
    p = (int *)malloc(10*sizeof(int));

    printf("%p\n",p);

    // 通过指针变量p来操作申请到的空间
    p[0] = 100;
    *(p+1) = 20;
    printf("%d",p[0]);
    // 申请了之后我们需要进行空间的释放
    free(p);
    
    // 将内存释放了之后,通常将指针赋值为NULL
    // 防止指针变为野指针
    p = NULL;

    return 0;
}

When we execute p=NULL, an error will occur if we continue to operate the pointer p. This is to prevent the wild pointer from modifying the content of the space pointed to by other pointers.

realloc function

#include <stdlib.h>
void *realloc(void *ptr,size_t size)

When the allocated space is not enough, you can use the realloc function to allocate memory space, allocate a new memory space specified by size on the heap, the size of the space is in bytes, and copy the content in the space pointed to by ptr to the new memory space, and finally returns the first address of the new memory space.

code show as below:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main()
{
    
    
    char *p;
    p = (char *)malloc(10);
    strcpy(p, "hello"); //向分配的空间中拷贝字符串
    printf("%p\n", p); // 在堆上申请的地址
    printf("%s\n", p); // 拷贝的内容

    // 当上面分配的空间不够大时,使用realloc函数重新分配新的空间
    p = (char *)realloc(p, 20); 
    printf("%p\n", p);
    printf("%s\n", p);
    
    //注意:分配的新的空间的首地址有可能有之前分配的空间首地址一样,也有可能不一样
    strcat(p, " world"); //追加字符串
    printf("%s\n", p);
    return 0;
}

At the time of reallocation, his space address may or may not be the same as the original address.

The header file here <string.h>is related to the processing of strings. We will explain it later here. It is enough to know its function here.

Guess you like

Origin blog.csdn.net/m0_67021058/article/details/130668621