数组的交换函数

发布于 2024-12-15 10:37:10 字数 491 浏览 2 评论 0原文

我们得到的信息:

1)定义一个数组 a[1000]a 是指针地址。

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.

现在,当我为 a 的两个条目调用此函数时,说 a[i],a[j] ...会发生什么?该函数是否由于 C/C++ 的某些内部构造而接收数组的两个单元的地址,或者是否接收指向 a[i]a[ 的指针的地址j]

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] ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

爱给你人给你 2024-12-22 10:37:10

a[i] 计算结果为对第 i 个元素的引用。它相当于 *(a+i),其中 a+i 是指向第 i 元素的指针。

引用内部如何工作是实现定义的(您不应该关心),但大多数(所有)编译器在内部使用指针。在这种情况下,它们将是指向数组中两个元素的指针。

a[i] evaluates to a reference to the ith element. It is the equivalent of *(a+i), where a+i is a pointer to the ith 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.

日记撕了你也走了 2024-12-22 10:37:10

我想说的是,它在幕后会收到指向 a[i]a[j] 的指针。

在以下两个程序上运行 g++ -S 会产生相同的结果:

#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;
}

并且

#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;
}

我使用 extern "C" 能够对输出进行比较,否则,损坏会有所不同。

旁注,当您编写例如 a+42 时,编译器会将地址计算为 a+sizeof(int)*42,同时考虑到 a code> 是一个指向 int 的指针。此特定示例在生成的汇编源代码中显示为 addl $168, %eax

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 e.g. 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.

鲸落 2024-12-22 10:37:10

A) C 和 C++ 是两种不同的语言。给定您的 swap(int &c, int &b) 方法定义,它是 C++

B) 因为它是 C++ 并且您正在传递引用,所以您将获得对数组元素(在内存中位于 a + i

如果这是 C,您将把函数定义为 swap(int *c, int *d) 并且你会传递指针a + i 因为数组自动降级为指针。

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.

写给空气的情书 2024-12-22 10:37:10
  1. 定义一个数组a[1000]a是指针地址。

不,不是。 a 是一个数组。在许多情况下,它衰减为指向第一个元素的指针,但它不是指针的地址(当然,除非您创建了一个指针数组)。

  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).

夜光 2024-12-22 10:37:10

首先,您的交换函数不是一个好主意,因为总和的值可能会溢出。只需使用临时变量即可。

当调用 swap(a[i], a[j]) 时,函数的参数是指向内存位置 a[i] 和 a[j] 的两个指针。指针包含两个 int 的地址。函数 swap() 不会有两个 int 位于同一数组中的概念。

将 c 和 d 声明为引用类似于传递指针,但是,您只能使用存储在该内存位置中的值(相当于取消引用指针),而不能更改指针指向的地址。

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.

寂寞笑我太脆弱 2024-12-22 10:37:10

仅当数字之和在 int 可以容纳的值范围内时,交换两个数字而无需 temp 的想法才有效。(通常为 power(2,sizeof(int)))。否则会发生溢出。
说到这个问题,

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

正如您在这里提到的,A是一个指针,A[i]是以A作为基地址形成的数组。
在c中,当你在内部说p[i]时,它会被转换为*(p+i),其中p是基地址。类似地,当你传递值的引用地址时。

注意:引用是隐式常量,引用必须在声明时指定值。

引用的作用类似于隐式取消引用的 const 指针。传递引用比传递指针更安全,因为使用指针可能会导致段错误。(在没有动态分配内存的情况下)

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)

尐偏执 2024-12-22 10:37:10

a[i] 表示值,因此 &a[i] = a + i 将被(内部)传递。对于a[j]也是如此。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文