Application Programming in Linux Environment (2): Memory Allocation

I. Overview

        Many system programs need to allocate additional memory for dynamic data structures (linked lists and binary trees). The size of such data structures is determined by the information obtained at runtime.

        The process can allocate memory by increasing the size of the heap. The so-called heap is a continuous virtual memory of variable length, which starts at the end of the uninitialized data segment of the process, and increases or decreases as the memory is allocated and released. Usually the current memory of the heap is changed. The boundary is called "program break".

Two: allocate memory on the heap

1. Adjust program break

#include <unistd.h>

int brk(void *addr);

void *sbrk(intptr_t increment);

       Changing the size of the heap is actually like instructing the kernel to change the program break position of the process. After the program break position is raised, the program can access any memory address in the newly allocated area while the physical memory page has not yet been allocated. The kernel will automatically allocate new physical memory pages when the process first attempts to access these virtual memory addresses.

brk: The system call will set the program break to the position specified by the parameter addr. Since the virtual memory is allocated in units of pages, addr will actually be rounded to the next memory page boundary. If the addr value is lower than the initial value &end, it will cause unpredictable behavior.

sbrk: Increase the incoming size of the parameter increment on the original address of the program break, and the call successfully returns a pointer to the starting address of the newly allocated memory.

2, malloc() and free()

#include <stdlib.h>

void *malloc(size_t size);
void free(void *ptr);

malloc: allocates memory of the size of the parameter size subsection on the heap, and returns a pointer to the starting address of the newly allocated memory. The allocated memory is not initialized.

free: The ptr parameter points to the memory block, which is generally the return value of the malloc call.

3. Calloc() and realloc()

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

calloc: nmemb specifies the number of allocated objects, and size specifies the size of each object. The return value is a pointer to the memory. If the allocation fails, it returns NULL. The memory allocated by calloc has been initialized to 0.

realloc: Adjust the size of the memory based on the existing memory.

Both of the above two memory request calls are released by free().

4. Allocate aligned memory: memalign() and posix_memalign()

#include <stdlib.h>
int posix_memalign(void **memptr, size_t alignment, size_t size);


#include <malloc.h>
void *memalign(size_t alignment, size_t size);

Three: allocate memory on the stack

#include <alloca.h>

void *alloca(size_t size);

The alloca() call can also dynamically allocate memory, but instead of allocating memory from the heap, it allocates it from the stack by increasing the size of the stack frame. You cannot use free to release, it will be released automatically as the stack frame is removed.

 

Guess you like

Origin blog.csdn.net/qq_34968572/article/details/109508117