Considerations for the use of realloc functions in C language

Source: Internet
Author: User

Recently learning C language, in the use of realloc function in addition to some problems, always find the problem, and then step by step debugging, finally found the problem, because the previous calloc function used to set the length of the string is wrong, resulting in the use of realloc at the end of the original string ' \ "was cleared , led to a series of problems, fortunately finally solved, now to summarize

ReAlloc Use Precautions (this is to summarize the experience of netizens)

1. When realloc fails, return null

2. ReAlloc failure, the original memory does not change, that is, no free or move, (this place is prone to error)

3. If there is enough memory behind the original memory, realloc memory = The original memory + the remaining memory, realloc or return the original memory address; If there is not enough memory behind the original memory, REALLOC will request new memory, and then copy the original memory data into the new memory, the original memory will be free, realloc return the address of the new memory

4. If size is 0, the effect is equal to free ()
5. The pointer passed to ReAlloc must have been previously assigned through malloc (), calloc (), or realloc ()

on MSDN

Return Value

ReAlloc returns a void pointer to the reallocated (and possibly moved) memory block.

If There is not enough available memory to expand the block to the given size, the original block was left unchanged, and C0>null is returned.

If size is zero, then the block pointed to by Memblock are freed; The return value is NULL, and Memblock is Left pointing at a freed block.

The return value points to a storage space which is guaranteed to being suitably aligned for storage of any type of object. To get a pointer to a type other than void, with a type cast on the return value

Remarks

The realloc function changes the size of an allocated memory block. The memblock argument points to the beginning of the memory block. If memblock is NULL, realloc behaves the same as malloc and allocates a new block of size bytes. I F Memblock is not NULL, it should are a pointer returned by a previous call to Calloc, malloc, or rea Lloc

Considerations for the use of realloc functions 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.