c language memory interface
#include <stdlib.h> void *malloc(size_t size);
Array and calloc
Use calloc to request data structures with fixed memory size
void *r; r = calloc(2,sizeof(struct tmap)); if(!r) { perror("error of calloc"); exit(EXIT_FAILURE); }
2 means to apply for two tmap spaces. The space here is continuous, otherwise it is not an array. You can use r[0], and r[1] to use the space.
Resize memory array
#include <stdlib.h> void *realloc(void *ptr, size_t size);
Use calloc to apply for data structures with fixed memory size, and then realloc to reallocate:
void *p; p = calloc(1,sizeof(struct map)); if(!p) { perror("error of xmalloc"); exit(EXIT_FAILURE); } p = realloc(p,sizeof(struct tmap));
The following are examples and encapsulated functions
#include <stdlib.h> typedef struct tmap { int num; int a; char con[128]; char x[2048]; }tmap; void xmalloc(size_t size) { void *p; p = malloc(size); if(!p) { perror("error of xmalloc"); exit(EXIT_FAILURE); } return p; } void * xcalloc(size_t size) { void *p; p = calloc(1,size); if(!p) { perror("error of xmalloc"); exit(EXIT_FAILURE); } return p; } int main() { printf("start..."); char *p; p= malloc(2048); if(!p) perror("malloc error"); free(p); tmap * map = malloc(sizeof(tmap)); if(!map) perror("malloc map error"); free(map); void *r; r = calloc(2,sizeof(struct tmap)); if(!r) { perror("error of calloc"); exit(EXIT_FAILURE); } r = realloc(r,sizeof(struct tmap)); if(!r) { perror("error of realloc"); exit(EXIT_FAILURE); } free(r); printf("over\n"); }
Execute under vscode to get the structure
No launch between start and over indicates success
problem
1 reassign size
Does the data exist after realloc reallocates the size? Standard c tells us that data still exists and there is no need to copy manually. The significance of reallocating memory size lies in two points:
1 reduce memory and reduce unnecessary memory space
2 increase memory to meet the need for more memory
So the problem still exists. When increasing the memory consumption, does the previous data still exist? The answer is still there. If the system cannot apply for more memory in the previous space, it will query the continuous memory, and there are potential copy actions, which requires cpu consumption. Therefore, if the memory pool is used, try to apply for enough memory at the beginning,
#include <stdlib.h> #include <stdio.h> typedef struct tmap { int num; int a; char con[128]; char x[2048]; }tmap; int main() { tmap *r, *s; r = calloc(2, sizeof(struct tmap)); if (!r) { perror("error of calloc"); exit(EXIT_FAILURE); } printf("r0 num is: %d\n", r[0].num); printf("r0 a is: %d\n", r[0].a); r[0].num = 100; r[0].a = 1000; s = realloc(r, sizeof(struct tmap)); if (!s) { perror("error of realloc"); free(r); exit(EXIT_FAILURE); } printf("s0 num is: %d\n", s[0].num); printf("s0 a is: %d\n", s[0].a); free(s); printf("over\n"); }
Output:
so
1 during initialization, the calloc function helps us clear the memory
After reallocating memory, the data does not change
3 whether the memory is not copied or has been copied, it is transparent to us.
2 memory alignment
8-byte alignment on 32-bit systems
64 bit system 16 byte alignment
If you do not align the bytes in advance, you'd better align the bytes in advance to avoid making mistakes in the address when you manually calculate later.
In most cases, the compiler and c library will automatically handle the alignment problem. The memory space returned by the three functions, malloc and calloc, and realloc, are aligned.
3 large memory applications
Use the mmap function to map disk and memory