ReAlloc () function parsing in C language

Source: Internet
Author: User

First, the basic characteristics

1. The realloc () function can reuse or extend memory previously allocated with the malloc (), Calloc (), and realloc () functions themselves.

2. The realloc () function requires two parameters: one is a pointer containing the address (the address is returned by the previous malloc (), calloc (), or realloc () function), and the other is the number of bytes of memory to be newly allocated.

3, the ReAlloc () function assigns the amount of memory specified by the second parameter, and copies the previously allocated content pointed to by the first parameter pointer to the newly provisioned RAM, and the copied content length equals the smaller one in the old and new memory area. That is, the new memory is larger than the original memory, the original memory is copied to the new memory, if the new memory is less than the original memory, only the length equal to the new memory space content.

4. If the first parameter of the ReAlloc () function is a null pointer, it is equivalent to the new memory space specified by the second parameter, which is equivalent to the malloc (), calloc (), or realloc () functions.

5. If the allocated memory is expanded, there are 3 scenarios:
If there is a required memory space behind the current memory segment, the memory space is expanded directly, and realloc () returns the original pointer.
If there is not enough free bytes behind the current memory segment, use the first block of memory in the heap that satisfies this requirement, copy the current data to a new location, and release the original data block, returning the new memory block location.
If the request fails, NULL is returned, and the original pointer is still valid.

Second, the matters needing attention

1. The first parameter is either a null pointer or a pointer to a previously allocated memory. If you do not point to previously allocated memory or point to memory that has been freed, the result is indeterminate.

2. If the call succeeds, regardless of whether the free space behind the current memory segment satisfies the requirement, the original pointer is released and a pointer is returned again, although the returned pointer may be the same as the original pointer, which means that the original pointer cannot be released again.

today, we are releasing the original pointer (that is, the first argument of the ReAlloc () function) This place made a mistake. Here is the test program. The function of the test program is very simple: to find where x should be inserted in an array sorted in ascending order, insert x into the array so that the array elements are still in ascending order.

1#include <stdio.h>2#include <stdlib.h>3 intMain ()4 {5     intn =0;6     inti =0;7     intindex =0;8     intInsert_data =0;9     int*pnumber =NULL;Ten     int*pnewarray =NULL; Oneprintf"Input Array size:\n"); Ascanf"%d", &n); -Pnumber = (int*)calloc(N,sizeof(int)); -     if(Pnumber = =NULL) the     { -printf"Not enough memory\n"); -Exit0); -     } +     //Enter array element hint information that was sorted in ascending order before inserting -printf"Input array:\n"); +      for(i =0; I < n; i++) A     { atscanf"%d", pnumber+i); -     } -     //Enter the element you want to insert X Tip information: -printf"Input x:\n"); -scanf"%d", &insert_data); -     //determine the index value of the position to be inserted in      for(i =0; I < n; i++) -     { to         if(Insert_data < * (pnumber+i)) +         { -index =i; the              Break; *         } $     }Panax Notoginseng     //allocate a new chunk of memory with ReAlloc () to store the original array and the newly inserted values -Pnewarray = (int*)realloc(Pnumber, (n+1)*sizeof(int)); the     if(Pnewarray = =NULL) +     { Aprintf"Not enough memory\n"); theExit0); +     } -     //Free (pnumber); $     //output The value of the newly allocated memory space to see if replication is implemented $      for(i =0; I < n+1; i++) -     { -printf"%4d", Pnewarray[i]); the     } -printf"\ n");Wuyi     //all data to be inserted and back is shifted backwards by 1 bits the      for(i = n; i > index; i--) -     { Wu* (pnewarray+i) = pnewarray[i-1]; -     } About* (Pnewarray + index) =Insert_data; $printf"After insert%d:\n", insert_data); -      for(i =0; I < n+1; i++) -     { -printf"%4d", * (pnewarray+i)); A     } +      Free(Pnewarray); the     return 0; -}

Note that code 44 lines out, and if commented out, the result is correct. If you do not comment out the error. Note 2nd of the precautions described above is correct. Here are the test cases and the results.

The picture on the left is the correct result of commenting out the code 44 lines, and the right picture is the wrong result without commenting out 44 lines of code.

I have always felt that this place is nothing, I did not expect to write a program today in this place fell into the hole inside. The more this unnoticed little place the more prone to make mistakes. Warning

ReAlloc () function parsing in C language

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.