17, Byte alignment problem
1. What is byte alignment
The basic unit of memory size is bytes. When reading and writing memory, cup does not read it byte by byte, but reads and writes memory in bytes multiple of 2, 4 and 8; 32-bit data can be read in one read cycle from the even address, while the 32-bit data can be obtained by reading two cycles from the odd address and splicing the data. The purpose of byte alignment is to improve efficiency. The alignment of different platforms is also different, and the information transmission process may be disordered.
2. Specify alignment values
The default number of alignments can be changed through the preprocessing instruction #pragma pack(n). N can be 1, 2, 4, 8, 16
#pragma pack (1) //size=17 #pragma pack (2) //size=18 #pragma pack (4) //size=20 #pragma pack (16) //size=24 //When the specified alignment value is larger than the largest basic type in the structure, the largest basic type in the structure is taken as the alignment value struct date { char year; int month; int day; double time; }; int main() { date d; int size = sizeof(d); printf("%d\n", size); return 0; }
18, Dynamic memory allocation malloc
-
Manually apply for a continuous memory space of a specified size from the heap using malloc function; free() is required to release after the application is completed
void *malloc (size_t size);//Allocate size by byte
int main() { int n = 0; int i = 0; int* ip = NULL; scanf_s("%d", &n); ip = (int*)malloc(sizeof(int) * n); if (NULL == ip) exit(1);//ip needs to be judged null for (int i = 0; i < n; i++) { ip[i] = i; } for (int i = 0; i < n; i++) { printf("%2d", ip[i]); } printf("\n"); free(ip);//At this time, ip is a null pointer ip = NULL;//After free, the ip still points to the first address of the open space, so you need to set the ip to null return 0; }
-
Although dynamic allocation is similar to array, it also has its own advantages. The size of array must be constant, and it cannot be run when its size exceeds the memory size of stack area. malloc is used to open up a continuous space, and its size can be customized, and scanf can be used for input. The size of the heap is much larger than the stack.
-
Four functions of dynamic memory management: malloc, calloc, realloc (to apply for space) and free (to release space)
-
When malloc application is successful, the pointer to newly allocated memory is returned. To avoid memory leakage, free() or realloc() must be used to reallocate the returned pointer; Null pointer will be returned if the application fails
-
The space applied by malloc has upper and lower bounds. Users can only use the space within the upper and lower bounds. The upper and lower bounds are the system default and cannot be modified
int main() { int* ip; ip = (int*)malloc(sizeof(int) * 5); if (ip == NULL) exit(1); for (int i = 0; i < 5; i++) { ip[i] = i; } return 0; }
[external chain picture transfer fails, and the source station may have anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-bn8hs0qe-1626341095438) (C: \ users \ dell.desktop-4bmd5h9 \ appdata \ roaming \ typora \ user images \ image-20210604111743752. PNG)] [external chain picture transfer fails, and the source station may have anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-sGMTKhCa-1626341095439)(C:\Users\DELL.DESKTOP-4BMD5H9\AppData\Roaming\Typora\typora-user-images\image-20210604111834487.png)]
-
int main() { int* ip = (int*)malloc(sizeof(int)); if (NULL == ip) exit(1);//We must judge the air space int* is = (int*)malloc(sizeof(int)); if (NULL == is) exit(1); *ip = 200; printf("%d\n", *ip); free(ip); //ip=NULL; *is = 1000; printf("%d\n", *ip); *ip = 100;//The ip still points to the first address of the space requested by malloc printf("%d\n", *ip); free(ip);//The same space cannot be a quadratic free space return 0; }//If the ip is not assigned NULL after free(ip), the ip will still point to the first address of the space opened up by malloc, and is may point to the same space as ip when opening up the space. As a result, ip and is can control this space at the same time, resulting in the danger of program hiding
-
malloc development space has header information (the size is 28 bytes), in which there is a mark for recording the size of development space. Therefore, when free is used, free can know how much space should be released, and the actual development space is larger than the real application space
-
Memory leak: when applying with malloc, the address of malloc space is lost, and if it is not released again, the pointer points to other addresses; Only malloc is not released, resulting in the use up of heap space
int main() { int* ip = (int*)malloc(sizeof(int)*5); if (NULL == ip) exit(1); int* ip = (int*)malloc(sizeof(int) * 10); } //Finally, only 40 bytes opened for the second time can be released, but the space opened for the first time cannot be released. Because its address is not recorded, it cannot be released and the address is lost
-
int *is=(int *)calloc(n,sizeof(int));
Assign a value to 0 in the open space
void *my_calloc(int num, int size) { void* vp = (void*)malloc(num * size); if (vp != NULL) { memset(vp, num, size);//memset(void *vp,int value,size num) } return vp; }
-
The difference between heap area and stack area:
difference Stack heap management style Automatically managed by the system Controlled by the programmer, it is easy to use, but easy to leak memory Growth direction Stack expansion to low address (bottom-up) The heap expands to high address, which is a discontinuous memory area Space size 1M or 10M Limited by virtual memory available in the computer Storage content After a function call, the local variable is out of the stack first, the instruction address is out of the stack, and finally the stack is balanced, and the execution continues downward from this point Heap is used to store data whose lifetime is independent of function, which is specifically managed by programmers Distribution mode The stack can be allocated statically or dynamically It can only be released dynamically and manually Distribution efficiency High; By the special register, the stack has special instructions Low; Provided by library functions, the mechanism is complex Fragmentation problem There is no fragmentation of the stack Heap space is released and allocated in a trivial way, resulting in discontinuous heap memory space, resulting in a large amount of fragmentation System response after allocation As long as the requested memory is less than the remaining memory of the stack, the system will provide memory for the program, otherwise an error will be reported