简体   繁体   中英

Swap function for Array

Information that we have:

1) defining an array a[1000] , a is the pointer address.

2)

void swap(int &c, int &b)
{
    c=c+b;
    b=c-b;
    c=c-b;
} 
// this is a method of swapping two variables without using temp variable.
// We use call by reference for the swap to actually take place in memory.

Now , when i call this function for a's two entries say a[i],a[j] ...what happens ?? Does the function receive the address of the two cells of the array due to some internal construct of C/C++ or does it receive the address of the pointers pointing at a[i] and a[j] ?

a[i] evaluates to a reference to the i th element. It is the equivalent of *(a+i) , where a+i is a pointer to the i th element.

How references work internally is implementation defined (you shouldn't care), but most(all) compilers use pointers internally. In this case they would be pointers to the two elements in the array.

I'd say that behind the scene it would receive pointers to a[i] and a[j] .

Running g++ -S on the following two programs produces identical results:

#include<iostream>
extern "C" void swap(int&c,int&b){
    c=c+b;
    b=c-b;
    c=c-b;
}
int main(){
    int*a=new int[1000];
    a[10]=10;
    a[42]=42;
    swap(a[10],a[42]);
    std::cout << a[10] << " " << a[42] << std::endl;
    delete[] a;
    return 0;
}

and

#include<iostream>
extern "C" void swap(int*c,int*b){
    *c=*c+*b;
    *b=*c-*b;
    *c=*c-*b;
}
int main(){
    int*a=new int[1000];
    a[10]=10;
    a[42]=42;
    swap(a+10,a+42);
    std::cout << a[10] << " " << a[42] << std::endl;
    delete[] a;
    return 0;
}

where I used extern "C" to be able to diff the outputs, otherwise the mangling differs.

Side note, when you write eg a+42 the compiler will calculate the address as a+sizeof(int)*42 , taking into account that a is a pointer to int . This particular example shows up as an addl $168, %eax in the generated assembly source.

A) C and C++ are two different languages. Given your swap(int &c, int &b) method definition, it's C++

B) Because it's C++ and you're passing references , you get a reference to the array element (which in memory is located at a + i )

If this were C you would have defined your function as swap(int *c, int *d) and you'd be passing the pointer a + i because array degrade to pointers automatically.

First of all, your swap function is a bad idea as the value of the sum might overflow. Just use a temp variable.

When you call swap(a[i], a[j]) the arguments to the function are two pointers to the memory locations a[i] and a[j]. The pointers contain the addresses of the two ints. The function swap() will have no concept of the two ints being in the same array.

Declaring c and d as references is similar to passing a pointer, however, you can only work with the values stored in this memory location (equivalent to dereferencing the pointer) but not change the address the pointer points to.

  1. defining an array a[1000] , a is the pointer address.

No it isn't. a is an array. In many cases it decays to a pointer to the first element, but it is not the address of a pointer (unless you made an array of pointers, of course).

The idea of swapping two numbers without temp works well only if sum of numbers is in the range of value ;a int can hold.(typically power(2,sizeof(int))).or else overflow will occur. Coming to the question,

int *a=new int;
a[1000];// if i have understood your question then....

As mentioned by you here A is a pointer and A [i] is array formed with A as base address. In c when you say p[i] internally it get converted as *(p+i) where p is base address.similarly when you pass by reference address of value is passed.

Note :References are implicitly constant ,references must be given value upon declaration.

References acts like a const pointer that is implicitly de-referenced.It is safe to pass references than that of pointers as using pointer may lead to segfaults.(where there is no dynamic allocation of memory)

a[i] represents the value so &a[i] = a + i will be passed (internally). Likewise for a[j] .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM