指针转换取消引用的指针有什么用?
这个问题是关于代码解释,而不是代码调试。我正在使用的代码有效。 我正在使用公共代码,我很好奇看看他们的“增长数组”模板之一,它看起来像这样:
template <typename TYPE>
TYPE *grow(TYPE *&array, int n, const char *name)
{
if (array == NULL) return create(array,n,name);
bigint nbytes = ((bigint) sizeof(TYPE)) * n;
array = (TYPE *) srealloc(array,nbytes,name);
return array;
}
函数 srealloc 看起来像这样:
void *Memory::srealloc(void *ptr, bigint nbytes, const char *name)
{
if (nbytes == 0) {
destroy(ptr);
return NULL;
}
ptr = realloc(ptr,nbytes);
if (ptr == NULL) {
error();
}
return ptr;
}
请忽略 create 暂时起作用。我的主要问题是为什么他们在模板中进行指针转换和取消引用数组?这样做有什么好处呢?如果他们根本没有 *&
怎么办?
谢谢!
This question is about code explanation, not code debugging. The code I'm using works.
I'm using a public code and I was curious to look at one of their "grow array" template, which looks like this:
template <typename TYPE>
TYPE *grow(TYPE *&array, int n, const char *name)
{
if (array == NULL) return create(array,n,name);
bigint nbytes = ((bigint) sizeof(TYPE)) * n;
array = (TYPE *) srealloc(array,nbytes,name);
return array;
}
and the function srealloc looks like this:
void *Memory::srealloc(void *ptr, bigint nbytes, const char *name)
{
if (nbytes == 0) {
destroy(ptr);
return NULL;
}
ptr = realloc(ptr,nbytes);
if (ptr == NULL) {
error();
}
return ptr;
}
Please disregard the create function for now. My main question is why do they pointer-cast and dereference array
in the template? What's the advantage of that? What if they just didn't have *&
at all?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
&
标记有多种含义,您在这里混淆了其中两种含义。你并不孤单!作为一个运算符,它的意思是“地址”,您似乎对此很满意(这来自C
)。但作为类型限定符,它的意思是“引用”,这是完全不同的。第一个含义:第二个含义:
在这个例子中,代码相当于
这个代码看起来不那么干净,但是对于
C
程序员来说更容易理解。所以......函数声明
(如您的示例中所示)意味着
f
可以更改调用函数传递的int*
参数的值。你的示例代码对我来说看起来有点奇怪,因为如果它可以直接更改参数,为什么还需要返回array
的新值?但这是一个风格问题,我已经学会不在这里讨论此类问题:-)The
&
token has many meanings, two of which you have confused here. You are not alone! As an operator, it means "address of", which you seem to be comfortable with (this comes fromC
). But as a type qualifier, it means "reference to", which is quite different. First meaning:Second meaning:
In this example, the code is equivalent to
This code doesn't look as clean, but it's easier to understand for
C
programmers.So...the function declaration
(as in your example) means that
f
can change the value of theint*
parameter passed by the calling function. Your sample code looks a bit screwy to me, because why does it need to return the new value ofarray
if it can change the parameter directly? But that is a question of style, and I have learnt not to discuss such matters here :-)不是“指针取消引用”。它是对指针的引用。需要它,以便增长函数可以更改指针,而不仅仅是指针指向的内容。另一种选择是指向指针的指针,如以下代码所示。
is not a "pointer-dereference". It's a reference to a pointer. It's needed so the grow function can change the pointer, rather than just what the pointer points to. The alternative would have been a pointer to a pointer, like in the following code.