Recently engaged in C Language development , Find yourself on average , I met some problems in my work C In language malloc The length of application is 0 The situation of , At this time, the returned pointer is not empty , It causes the problem of service terminal , At the same time, the length of the array in the structure is 0 The memory allocation is not very clear , Therefore, this paper summarizes other people's blogs and makes an in-depth analysis of their own opinions .

one ,malloc apply 0 Length memory

       
Initialize on heap malloc It's an area , however malloc() Of size by 0, This address is used to store a lot of data , The data is still available , But the stored data will trample on other addresses , It'll be there when you use another address segment
fault Segment error problem

In the standard malloc In implementation , The size of the input value is not checked , Instead, the input value is aligned and the space is allocated directly from the heap . 

   Regardless of the size of the input value , stay malloc The minimum internal memory allocation size of is a fixed value ( Generally 8B), because malloc This space is needed to maintain the linked list of memory blocks on the heap . So when a user applies for a piece of 0B Space time ,malloc What is the actual allocation of space 8B, If the space requested by the user is X, be malloc What is the actual allocation of space ( alignment (X)+8). That's why malloc The reason why the allocated space must not be used beyond the boundary : The internal list structure of the heap will be destroyed . 
   
   about new and delete malloc and free Such a memory allocation and release function : to the end delete and free How do you know how much memory to free ? 
   In fact new and malloc When memory allocation is successful , The system returns a pointer to the memory , A memory block is also obtained to record the size of memory allocated here .

In memory management , Memory is divided into two parts : Stack and heap

        The stack has its own machine instructions , It's an in out data structure . 

  malloc The allocated memory is heap memory , Because the heap does not have its own machine instructions , So the system should write its own algorithm to manage this memory , The usual practice is to use linked lists , Add a header in front of the allocated memory , It stores the starting address and size of the allocated memory , Your malloc Returns the starting pointer in the header , This address comes from a series of algorithms , Usually not for 0, Once the allocation is successful , Returns a valid pointer , The return pointer is NULL The condition is to apply a certain size of memory , When memory space is not enough , For distribution 0 In terms of space , The algorithm has worked out the starting address of the available memory , But you're taking over 0 space , So the operation on that pointer is wrong , The operating system generally does not know its termination address , Because of the occupancy size, the termination address can be deduced , And even if it's distributed 0 Space also needs to release it , In fact, it is a released list node .

for example :
#include <stdio.h> #include <stdlib.h> int main() { char* temp = NULL; temp =
malloc(0); printf("the address of temp = %p\n", temp); }
Output results :
the address of temp = 0x685010
* 1
explain malloc(0) Yes, I will return malloc Your address , just malloc The address length of is 0

summary : In a function , If the length of the application memory is the input parameter , It is necessary to judge whether it is greater than 0 , Otherwise, there will be unexpected mistakes .

two ,

        In the standard C and C++ in , Count Reg 0 Is prohibited . But in GNUC in , There is a very strange usage , That's the length 0 Array of , such as Array[0];
A lot of people may think it's incredible , Count Reg 0 It doesn't make sense to have an array of , But here it is , It means something else ,  This feature is not portable ,
therefore , If you're committed to writing portable , Or a little cross platform code , these ones here Trick It's better to put it away .
struct line { int length; char contents[0]; }; struct line *thisline = (struct
line *)malloc (sizeof (struct line) + this_length); thisline->length =
this_length;
       
This defines the main meaning of the code body , And initialize it , The second member variable of the structure above contents[0] In fact, it doesn't take up memory space , So the length of the whole structure sizeof(struct line) by 4. When using malloc When requesting memory space for it , As shown above , Apply for a length of structure length plus variable length of memory space to structure type pointer , At this time contents Point to the variable length memory space applied . Because it's a one-time application , Therefore, the variable length memory space is continuous with the memory space of the front structure . For this variable length memory space , It can be accessed by array . For the whole structure , When no longer in use , have access to free Function to release it at one time , You don't have to release them separately like a pointer .

summary :

* Count Reg 0 Does not occupy memory space , The pointer mode needs to occupy memory space .
*   For a length of 0 array , When applying for memory space , The principle of one-time distribution is adopted ; For structures that contain pointers , When you apply for space, you need to do it separately , It should also be released separately .
* For a length of 0 The array of can be accessed in the way of array .
 

 

 

Technology
©2019-2020 Toolsou All rights reserved,
2020 The 11th National Blue Bridge Cup C/C++b Group summary ( Completion ) Review of the most complete computer network principles in history vue-cli 3 VUE Scaffold project construction ( Detailed explanation ) solve Vue+TypeScript Under development TS Don't recognize this.$refs The question of Vue Using the function of anti chattering and throttling How to use division operation in relational algebra SQL Statement representation ?copy-webpack-plugin Copy and compress files avue The use of dictionaries in English Teaching girls to learn Java: What is? Java?python Code painting Cherry Blossom -python Draw cherry tree code Specific code introduction