Memory Management of inet_ntoa () in C Language

Source: Internet
Author: User

When debugging a program today, the output results are always incorrect. I don't know why. The Code is as follows:

# Include <sstream> <br/> # include <iostream> <br/> # include <netinet/in. h> <br/> # include <ARPA/inet. h> <br/> using namespace STD; <br/> void print_address (struct in_addr addr1, struct in_addr addr2) <br/>{< br/> ostringstream OSS; <br/> OSS <inet_ntoa (addr1) <"|" <inet_ntoa (addr2); <br/> cout <OSS. STR () <Endl; <br/>}< br/> int main () <br/>{< br/> struct in_addr addr1, addr2; <br/> inet_aton ("10.0.0.1", & addr1); <br/> inet_aton ("10.0.0.2", & addr2); <br/> print_address (addr1, addr2 ); <br/> return 0; <br/>}

I thought the program would output:

10.0.0.1 | 10.0.0.2

As expected, the output result is:

10.0.0.1 | 10.0.0.1

The code is changed to the following for testing:

Void print_address (struct in_addr addr1, struct in_addr addr2) <br/>{< br/> ostringstream oss; <br/> oss <inet_ntoa (addr1) <"|" <inet_ntoa (addr2); <br/> cout <oss. str () <endl; <br/> cout <inet_ntoa (addr1) <endl; <br/> cout <inet_ntoa (addr2) <endl; <br/>}

The output is still very strange:

10.0.0.1 | 10.0.0.1 <br/> 10.0.0.1 <br/> 10.0.0.2

Is it because of the oss issue or other issues? Modify the Code as follows:

Void print_address (struct in_addr addr1, struct in_addr addr2) <br/>{< br/> ostringstream oss; <br/> oss <inet_ntoa (addr1) <"| "; <br/> oss <inet_ntoa (addr2); <br/> cout <oss. str () <endl; <br/> cout <inet_ntoa (addr1) <endl; <br/> cout <inet_ntoa (addr2) <endl; <br/>}

At this point, the program outputs the expected results in a strange way:

10.0.0.1 | 10.0.0.2 <br/> 10.0.0.1 <br/> 10.0.0.2

Based on the above output results, I roughly guessed the cause and added the following code to the source code:

Void print_address (struct in_addr addr1, struct in_addr addr2) <br/>{< br/> ostringstream oss; <br/> oss <inet_ntoa (addr1) <"| "; <br/> oss <inet_ntoa (addr2); <br/> cout <oss. str () <endl; <br/> printf ("% p/n", inet_ntoa (addr1), inet_ntoa (addr2 )); <br/> printf ("% p/n", inet_ntoa (addr1); <br/>}

As expected, program output:

10.0.0.1 | 10.0.0.2 <br/> 0x7fed7533b6b8 0x7fed7533b6b8 <br/> 0x7fed7533b6b8

Now I can confirm my guess:

Oss <inet_ntoa (addr1) <"|" <inet_ntoa (addr2); when running, run inet_ntoa (addr2) to obtain a function pointer, assuming p1; then run inet_ntoa (addr1) to get another pointer, assuming p2, and then run the entire statement oss <p1 <"|" <p2; to get the final result. The problem is that, in fact, p1 and p2 values are equal, that is, they point to the same memory address, and the data stored in the memory of this address space is the last execution of inet_ntoa (). The oss statement is executed from right to left. Therefore, when oss <p1 <"|" <p2; is executed: oss <p1 <"|" <p1; therefore, oss stores two identical IP addresses.

Think about it. system developers also have their own principles for such design. Because the inet_ntoa () function call does not require manual memory allocation, the system must allocate sufficient space for this operation. If a space is allocated every time this function is executed, but the user has not released it, or the system has released it once, and the user has released it again, memory leakage will occur. Therefore, developers can simply open up a piece of memory space and use the same memory for each call. The system will reclaim the memory at the end of the process, which is not a waste, you do not need to perform tedious operations.

However, there is another problem. If you manually release the memory segment, the problem will also occur. Therefore, we recommend that you do not use similar functions, but use functions that allow you to customize the memory address, here, the inet_ntoa () function corresponds to inet_ntop ().

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.