arrays and pointers as function parameters-high-quality C, C + + Programming Guide

Source: Internet
Author: User

This article is based on the high quality C, C + + Programming Guide. Understanding after reading, and then through your own words and examples to clear, this is the way to learn. The following things are what they think, if anything wrong, I hope that the great God pointed out.


1. When the array is passed as a function parameter, the array is automatically degraded to the same type of pointer



Results:


In the first figure, Str is an array with a length of 6, and as a function parameter is degraded to a pointer, and the length of the pointer is fixed to 4.

In the first picture, STR is an array, so it is not possible to self-increment or decrement str, but in the function, it can be degraded to a pointer, while pointers can be self-increment.


2, the pointer parameter is how to pass the memory

The pass-through value is passed and the reference is passed, and the pointer parameter is a useful pass. There is nothing wrong here, look at the following analysis.


As you can see, the address that the argument pstr and the parameter P point to is the same (all 3866140), and the Addresses of PSTR (3866128) and P (3865916) are not the same.

Cause: The compiler makes a temporary copy of each parameter of the function, the copy of the pointer parameter P is _p, and the compiler causes _p=p. Therefore, the formal parameter p is a copy of the argument pstr, all pointing to Str. The program in the function body modifies the contents of the _p, causing the contents of the parameter p to be modified accordingly, which is why the pointer can be used as an output parameter.


3. Using pointer parameters to pass memory

Using pointers as parameters, request a memory interval for the pointer in the function.



The above example will fail to request memory. Because the formal parameter p is just a copy of STR, let P point to the newly requested memory, which changes the point of P instead of changing the content that P originally pointed to Str. When P points to the new memory, P has no relation to Str.

The parametric is released at the end of the function, causing the memory requested on the heap to not be released, so a function is lost without executing a single memory.


If you have to use pointer parameters to request memory, you should instead use a pointer to pointer.

The relationship between pointers and pointers is understood through a graph:



Use the pointer pointer to request memory:



It is successful to request memory for STR at this time. As stated above, the compiler generates a copy of the pointer when it is made, and the content is the same.

So, pp is a copy of the PPSTR, the formal parameter pp and the argument ppstr point to the same content str, then the *PP content is STR, the operation of *PP is the operation of STR, *PP points to the new application of memory, so STR also points to the memory.

Remember the last line of code to release the memory requested on the heap, otherwise it will cause a memory leak.


Still do not understand, you can look at the following diagram:


arrays and pointers as function parameters-high-quality C, C + + Programming Guide

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.