Difference and advantages and disadvantages of transfer value and address

Source: Internet
Author: User

Blogging for the first time may be a bit messy, so let's start with an example.

Objective: Take a simple example as an example, write a function to realize the exchange of two variables first look at the correct method, in C language as an example

#include <stdio.h>

void f (int* x,int *y);

int main (void)

{

int a=3,b=4;

f (&a,&b);

return 0;

}

void f (int * X,int *y)

{

int temp;

Temp=*x;

*x=*y;

*y=temp;

}

This can achieve the functions we need, so many students in the beginner may make the following mistakes, of course, I have made

#include <stdio.h>

void f (int x,int y);

int main (void)

{

int a=3,b=4; f (&a,&b);

return 0;

}

void f (int x,int y)

{

int temp;

Temp=x;

X=y; Y=temp;

}

After everyone ran, we found that the value of a and B were not interchangeable. Mainly in the f (int x,int y) function, variable x and variable y are independent existing int type variables, that is, X and Y are the same as a,b in memory allocated memory, when the main function calls the F () function, we simply change the value of the &x point in memory to B , the address in the memory that &y points to is replaced by a, and no action is taken on the values in &a and &b memory, which means that you are working on another two separate memory spaces that are not associated with &a and &b, so naturally you cannot swap the values of A and B.

So why is the first method possible? Since the F () function is invoked at this time, the incoming formal parameter is &a and &b, so that is to say x=&a,y=&b, then obviously *x and &a are a concept, and the operation *x and *y are actually the * (&a) and * (& b), so when swapping *x and *y, the value of a and B is actually exchanged.

Then some students will ask the value and the location of the advantages and disadvantages of it, it is obvious, the value of the transfer you need to allocate additional memory space for your formal parameters, when your parameters occupy a large amount of memory such as 100 bytes, this is very memory. So the advantage of the address is reflected, we know that under the WINDOWS32 bit, address bus select、read is 32, that is, the address is 4 bytes 32 digits, so no matter what type of data your formal parameter is the address, I just passed the first byte of the data, that is, 4 byte data, This saves a lot of memory.

The first time to write a blog, this is also their own in the study of some insights, I hope everyone to criticize ~ ~

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.