指针转换取消引用的指针有什么用?

发布于 2024-12-07 07:59:18 字数 793 浏览 0 评论 0原文

这个问题是关于代码解释,而不是代码调试。我正在使用的代码有效。 我正在使用公共代码,我很好奇看看他们的“增长数组”模板之一,它看起来像这样:

  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 技术交流群。

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

发布评论

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

评论(2

〆一缕阳光ご 2024-12-14 07:59:18

& 标记有多种含义,您在这里混淆了其中两种含义。你并不孤单!作为一个运算符,它的意思是“地址”,您似乎对此很满意(这来自C)。但作为类型限定符,它的意思是“引用”,这是完全不同的。第一个含义:

int x ;
int* p = &x ; // p = address of x (as in C)

第二个含义:

void f (int& x) { // x is a reference to an int -- its address is passed to f
  x++ ;
}
...
int y = 99 ;
f (y) ; // After this call, y is equal to 100

在这个例子中,代码相当于

void f (int* x) {
  (*x)++ ;
}
...
int y = 99 ;
f (&y) ; // After this call, y is equal to 100

这个代码看起来不那么干净,但是对于 C 程序员来说更容易理解。

所以......函数声明

void f (int*& p) ;

(如您的示例中所示)意味着 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 from C). But as a type qualifier, it means "reference to", which is quite different. First meaning:

int x ;
int* p = &x ; // p = address of x (as in C)

Second meaning:

void f (int& x) { // x is a reference to an int -- its address is passed to f
  x++ ;
}
...
int y = 99 ;
f (y) ; // After this call, y is equal to 100

In this example, the code is equivalent to

void f (int* x) {
  (*x)++ ;
}
...
int y = 99 ;
f (&y) ; // After this call, y is equal to 100

This code doesn't look as clean, but it's easier to understand for C programmers.

So...the function declaration

void f (int*& p) ;

(as in your example) means that f can change the value of the int* 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 of array if it can change the parameter directly? But that is a question of style, and I have learnt not to discuss such matters here :-)

为你拒绝所有暧昧 2024-12-14 07:59:18
*&

不是“指针取消引用”。它是对指针的引用。需要它,以便增长函数可以更改指针,而不仅仅是指针指向的内容。另一种选择是指向指针的指针,如以下代码所示。

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;
}
*&

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.

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