Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6635581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:04:21+00:00 2026-05-25T23:04:21+00:00

This question is about code explanation, not code debugging. The code I’m using works.

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T23:04:22+00:00Added an answer on May 25, 2026 at 11:04 pm

    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 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm not talking about making portable code. This is more a question of distribution.
This question is partly about delegates, and partly about generics. Given the simplified code:
I have seen this question about deploying to WebSphere using the WAS ant tasks.
In the accepted answer to this SO question there is an explanation about structural
I have couple of questions about AS3 variables handling by AVM/compiler/scope .1. This code
This question about Timers for windows services got me thinking: Say I have (and
Followed this question about delayed_job and monit Its working on my development machine. But
This question is a follow up to my previous question about getting the HTML
I was reading this question about how to parse URLs out of web pages
I just came across this question about initializing local variables. Many of the answers

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.