C language ReAlloc Why should have return value, realloc return value specific explanation/(solve random length string input problem).

Source: Internet
Author: User

A large amount of memory operations are used in C-language operations, when one of the most frequently used realloc ().


It is the literal meaning to know that the function is used to allocate memory again.

Use the following methods for example:

newptr= (data type *) ReAlloc (oldptr,memsize)


The oldptr points to a pointer to allocate memory again.

Newptr Pointer to the newly allocated space.

Memsize is the space size after allocation.


The use of this function involves several issues:

1. Return values in different situations

2.OldPtr points to the memory will not voluntarily release

3.OldPtr and newptr what are their respective content and what are they related to


To answer this question, you can see from the action flow of the function:

The function is based on whether the memory satisfies the function requirement and completes one of the following 3 actions:

1. Check that the available memory behind the block of memory that Oldptr points to meets the requirements of this realloc, assuming that the available memory is also allocated to the memory block-the pointer points to the same position, the contents of the memory block are unchanged, and the memory block expands. In this case, the return value newptr = oldptr;

2. assume that the oldptr points to a memory block after the available memory is not realloc enough. Then complete the following three steps <a> will allocate another memsize of memory in available memory and return the start address of this memory to newptr. <b> copies the contents of the OLDPTR memory block to the NEWPTR memory block. The value of the <c>oldptr is constant, but the block of memory it points to is freed (note: It is self-releasing without the need to invoke free).

The case of the author after a run of action 2

in the figure the role of the program is: input of random length strings -Each round of the loop enters a character, appends the character to the previously entered character, makes it a string, and correspondingly increases the memory space occupied by the string.

I entered the ABCD in turn ....

When inputting the character ABC, the result after the round loop runs ReAlloc is that thevalue of 1.STR and Newstr is the same as 0X00751CB8. 2. It points to the same content as "ABC"

In the loop entered into the character D, the result of running realloc is that the value of1STR differs from the value of NEWSTR. 2 at the same time, it is possible to find that the content of STR is reset to " hafnium hafnium hafnium hafnium ..." to see its corresponding memory unit can be found, will be free reset memory 0xEE 0xfe 0xee 0xFE ... The sequence, for example, as seen in the.


3. Assume that there is no memory in the entire heap, no matter what, to meet the requirements of realloc. The allocation fails and the return value is null.

Conclusion: It is improper to use the same pointer for Oldptr and NEWPTR in realloc use in the program. It is more appropriate to use the procedure in the appropriate program. Since the allocation failure can be avoided, the original pointer is lost after NULL is returned. You can also get the memory block that the old pointer and the new pointer point to at the same time.


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.