C + + Learning Note (ii)--swap function (swap)

Source: Internet
Author: User

This time we're going to go through a simple function swap to understand the nature of the function's parameters and how to choose the way to pass the parameters in C + +.

Let's look at the first procedure:

void swap (intint  y) {    int temp = y;     = x;     = temp;}

Through the invocation of the main function, we find that x, Y does not implement the interchange:

int Main () {    int1;     int Panax Notoginseng ;    Swap (x, y);     " : " << y << Endl;     return 0 ;}

The reason is that plastic x and Y are passed by value within the function swap, and when passed by value, the function does not access the currently invoked argument. The function handles the value of its local copy, which is stored in the run stack, so changing these values does not affect the value of the argument. Once the function is finished, the function's active record pops out of the stack, and the local values disappear.

The contents of the arguments are not changed in the case of passing by value. This means that the programmer does not need to save and restore the value of the argument when the function is called. If no mechanism is passed by value, then each parameter that is not declared as const may be changed with each function call. The least harm is done by value and requires the least amount of work for the user. There is no doubt that passing by value is a reasonable default mechanism for parameter passing.

Also, if a variable that is an argument is an object of a large class, the time and space overhead that is allocated and copied to the stack is often too large.

What should we do to achieve the effect of the swap function? The first feasible approach is to declare a formal parameter as a pointer:

void pswap (intint *y) {    int temp = *y;     *y = *x;     *x = temp;}

In the Pswap function, the memory address (pointer) of the two variables is passed so that we can manipulate the corresponding values directly. In fact, there is still the problem of passing by value, except that the original shaping pass becomes a pointer pass. We can modify the memory pointed to by the pointer but still cannot modify the pointer itself. A second possible approach is to think of a reference to a parameter declaration as a pointer:

void prswap (intint *&y) {    int temp = *y;     *y = *x;     *x = temp;} void prswap (intint *&y) {    int *temp = y;     = x;     = temp;}

Note that I have provided two function definitions under the same function prototype. Either way, the pass-by-value problem does not occur during the pass-through phase of the argument. So which of the two definitions is more satisfying to our needs:

(1) Swapping values in memory

(2) Exchange pointer address

If you consider the requirements of this article separately, the first method is more satisfying. However, if we need to exchange a large class object, the second is more efficient.

Summary: Memory management is a difficult point of C + + learning, beginners are often not easy to master. But the more so, the more you can embody a developer's language skills.

C + + Learning Note (ii)--swap function (swap)

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.