malloc(0)

Recently saw a very strange question, is malloc (0), the return is not NULL, but free this memory Shique error.

Programs run up memory can be divided into the stack and heap, the stack has its own machine instructions, is out after an advanced data structures, I am here not too much to explain, malloc memory is allocated heap memory, because the heap is not own machine instructions, so there must write their own algorithms to manage system-chip memory, the usual practice is to use a linked list, adding a header before each sheet is allocated memory, which stores the start address and size of the allocated memory your start pointer returned by malloc is ahead of the table, the address is a series of algorithms come up, usually not to zero, once the assignment is successful, it returns a valid pointer to allocate space for the 0 the algorithm has calculated the start address of available memory, but the space you occupy 0, so that the pointer operation is wrong, operating systems generally do not know the ending address, occupation because of the size can be launched end address, there is the even distribution 0 space should release it, in fact, released the list of nodes. Also, the returned pointer is the starting address of addresses available, the available size is fixed, is 56 bytes in VC6 , the sizes are possible node list, do a verification code is as follows:

int main ()
{
 char * PTR = NULL;
 char * ptr2 = NULL;
 char * ptr3 = NULL;

 PTR = the malloc (0 * the sizeof (char));
 ptr2 = the malloc (32 * the sizeof (char));
 ptr3 = the malloc (32 * the sizeof (char));

 // strcpy (PTR, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
 strcpy (ptr2, "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
 strcpy (ptr3, "ccccccccccccccccccccccccccccccc");

 the printf ( "PTR:% X \ n-", PTR );
 the printf ( "ptr2:% X \ n-", ptr2);
 the printf ( "ptr3:% X \ n-", ptr3);
 
 IF (PTR == NULL)
  the printf ( "GOT A NULL pointer \ n-");
 the else
 {
  the printf ( "GOT a! Valid pointer \ n-");
  //, 56 a, and another for storing a byte '\0'
  //strcpy(ptr,"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
   strcpy(ptr,"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");

  //printf("the value at %X is:%c\n",ptr,*ptr);
  //printf("the string at %x is :%s\n",ptr, ptr);
    printf("the string ptr: %s\n", ptr);
    printf("the string ptr2: %s\n", ptr2);
    //printf("the string ptr2 + 56: %s\n", ptr2 + 56);
    printf("the string ptr3: %s\n", ptr3);

  //free(ptr);
 }
 return 0 ;

}

The results are as follows:


After malloc (0) in the malloc (32), just return the address of both the phase difference 56byt, should maintenance node is VC6.0 memory size, and then with ptr2 difference ptr3 address 88 (56 + 32).


Comprehensive verification of the above, we can draw a conclusion that, if malloc succeeds, the return is actually starting address management chopsticks linked list, this is not going to address, but the address you can use 0 0 indeed, that the ptr2 address ptr + 56, malloc real address used is the address returned by malloc 56 plus the header size start to the end of the address sizeof. This is beyond the scope of cross-border access


Published 22 original articles · won praise 9 · views 8834

Guess you like

Origin blog.csdn.net/ljm_c_bok/article/details/80298797