数组的交换函数
我们得到的信息:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
a[i]
计算结果为对第i
个元素的引用。它相当于*(a+i)
,其中a+i
是指向第i
元素的指针。引用内部如何工作是实现定义的(您不应该关心),但大多数(所有)编译器在内部使用指针。在这种情况下,它们将是指向数组中两个元素的指针。
a[i]
evaluates to a reference to thei
th element. It is the equivalent of*(a+i)
, wherea+i
is a pointer to thei
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.
我想说的是,它在幕后会收到指向
a[i]
和a[j]
的指针。在以下两个程序上运行 g++ -S 会产生相同的结果:
并且
我使用 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]
anda[j]
.Running
g++ -S
on the following two programs produces identical results:and
where I used
extern "C"
to be able todiff
the outputs, otherwise the mangling differs.Side note, when you write e.g.
a+42
the compiler will calculate the address asa+sizeof(int)*42
, taking into account thata
is a pointer toint
. This particular example shows up as anaddl $168, %eax
in the generated assembly source.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 pointera + i
because array degrade to pointers automatically.不,不是。
a
是一个数组。在许多情况下,它衰减为指向第一个元素的指针,但它不是指针的地址(当然,除非您创建了一个指针数组)。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).首先,您的交换函数不是一个好主意,因为总和的值可能会溢出。只需使用临时变量即可。
当调用 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.
仅当数字之和在 int 可以容纳的值范围内时,交换两个数字而无需 temp 的想法才有效。(通常为 power(2,sizeof(int)))。否则会发生溢出。
说到这个问题,
正如您在这里提到的,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,
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]
表示值,因此&a[i]
=a + i
将被(内部)传递。对于a[j]
也是如此。a[i]
represents the value so&a[i]
=a + i
will be passed (internally). Likewise fora[j]
.