The use of ReAlloc function __ function

Source: Internet
Author: User

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace Std;
void Setval (char *tmp,int qs,int num,char mm)
{
for (int i=qs;i<num-1;i++)
{
* (tmp+i) =mm;
}
* (tmp+num-1) = ' ";
}
int main ()
{
Char *p,*q;



Shorten memory
p = (char *) malloc (1000);
Setval (p,0,1000, ' a ');
Q = (char *) realloc (p,800);
if (q!=null)
{
Setval (q,795,800, ' S ');
cout<<q<<endl;
cout<< "Success" <<endl;
}
Else
{
Setval (p,500,800, ' F ');
cout<< "Fail" <<endl;
}
Free (q);
GetChar ();
GetChar ();
System ("pause");
return 0;
}

ReAlloc can enlarge or shrink the space indicated by the given pointer, and the contents of the original memory will remain unchanged. Of course, for shrinking, the content of the part that is shrunk is lost.

ReAlloc does not guarantee that the adjusted memory space retains the same memory address as the original memory space. Conversely, the pointer returned by ReAlloc is likely to point to a new address:

Because ReAlloc allocates memory from the heap, when a chunk of memory is enlarged, the realloc gets the additional bytes directly from those bytes behind the existing data on the heap, but if the byte behind the data is not enough, then the first free block with enough size on the heap is used. The existing data is then copied to the new location, and the old block is put back on the heap.
In the code, if we use the int* memory mode of i = (realloc) (I, 2*sizeof (int)), there are two scenarios:

1. Assign success, return void* pointer: realloc return value if the value is different from the previous malloc, then the ReAlloc function is complete, I point to the old memory automatically free.
2. Allocation failure, return null value: At this time, I originally pointed to the memory has not been free, and now can not find the address, this appears memory leak.

Workaround: Define another pointer J to receive the ReAlloc return value, determine if it succeeds, and succeed to assign J to I


Article from: http://blog.csdn.net/leopardaa521/archive/2009/10/12/4658887.aspx

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.