reference and pointer-reference

Discussion in 'C++' started by kasthurirangan.balaji@gmail.com, Dec 24, 2007.

  1. Guest

    Hi,

    Recently i came across a functiion declaration as below

    void f(char *&a);

    Nowhere i have come across this style. I understand, passing by
    reference is always better(to avoid copying), that too added with
    const like

    void f(const std::string &a); //since it was char *a, i assume it to
    be a string

    This i came across in the book "Secrets of C++ Masters". Though this
    book is old, i just wanted to know what it speaks of. I do not know
    whether this is still accepted or was a part of pre-standard c++. I
    tried a toy program with a function like this. It did compile fine and
    executed successfully. As per the book, this style prevents pointer
    copy(address copy) - is that true? atleast in c++. Pls advice.

    Thanks,
    Balaji.
    , Dec 24, 2007
    #1
    1. Advertising

  2. Rolf Magnus Guest

    wrote:

    > Hi,
    >
    > Recently i came across a functiion declaration as below
    >
    > void f(char *&a);
    >
    > Nowhere i have come across this style. I understand, passing by
    > reference is always better(to avoid copying),


    I wouldn't say "always".

    > that too added with const like
    >
    > void f(const std::string &a); //since it was char *a, i assume it to
    > be a string
    >
    > This i came across in the book "Secrets of C++ Masters". Though this
    > book is old, i just wanted to know what it speaks of. I do not know
    > whether this is still accepted or was a part of pre-standard c++. I
    > tried a toy program with a function like this. It did compile fine and
    > executed successfully. As per the book, this style prevents pointer
    > copy(address copy) - is that true?


    Yes. In the above function, the pointer is passed by reference. That way, f
    can change the pointer value. It could e.g. allocate dynamic memory and
    assign its address to a, which refers to the variable that was passed by
    the caller. If the paramter was just a char*, f()'s changes to it would
    only be local to the function, since a is just a copy of the pointer that
    was passed by the caller.
    Rolf Magnus, Dec 24, 2007
    #2
    1. Advertising

  3. On 2007-12-24 12:14, wrote:
    > Hi,
    >
    > Recently i came across a functiion declaration as below
    >
    > void f(char *&a);
    >
    > Nowhere i have come across this style. I understand, passing by
    > reference is always better(to avoid copying), that too added with
    > const like
    >
    > void f(const std::string &a); //since it was char *a, i assume it to
    > be a string
    >
    > This i came across in the book "Secrets of C++ Masters". Though this
    > book is old, i just wanted to know what it speaks of. I do not know
    > whether this is still accepted or was a part of pre-standard c++. I
    > tried a toy program with a function like this. It did compile fine and
    > executed successfully. As per the book, this style prevents pointer
    > copy(address copy) - is that true? atleast in c++. Pls advice.


    Yes, using a reference to a pointer allows the function to change what
    the pointer points to, which can be useful when the function allocates
    memory, though I would have preferred to return a pointer instead.

    Consider this:

    #include <iostream>

    void f(char* a)
    {
    a = "Hello World";
    }

    void g(char*& a)
    {
    a = "Hello World";
    }

    int main()
    {
    char* s = "Init";
    f(s);
    std::cout << s << "\n";
    g(s);
    std::cout << s << "\n";
    }

    Since f() did not take a reference to a pointer as argument it can not
    change what s points to, but g() can.

    --
    Erik Wikström
    Erik Wikström, Dec 24, 2007
    #3
  4. Daniel T. Guest

    wrote:

    > Hi,
    >
    > Recently i came across a functiion declaration as below
    >
    > void f(char *&a);
    >
    > Nowhere i have come across this style. I understand, passing by
    > reference is always better(to avoid copying), that too added with
    > const like
    >
    > void f(const std::string &a); //since it was char *a, i assume it to
    > be a string


    Be careful here. Passing by const reference (as in your example) is an
    optimization when you are dealing with an object that allocates memory
    or who's size is larger than sizeof( int ), but only if you don't end up
    copying the object inside the function anyway. The function declaration
    you came across, "reference to pointer to char" is not such a beast.
    Removing the 'const' qualifier makes it completely different.

    A non-const reference parameter is used create an "in/out" parameter. In
    other words, you can use that parameter to pass data into the function,
    and it can use the parameter to pass data back out to the calling code.

    Generally, non-const reference parameters are used as an optimization
    when the return value allocates memory or is larger than sizeof int (to
    avoid the copy... compilers can often do this particular optimization on
    their own though so it isn't that common,) or when a function needs to
    return multiple chunks of data.

    Let's say for example that you have a char* that contains a bunch fields
    separated by tabs (assume that other whitespace characters could be
    embedded in these fields,) and you want to break these fields up into a
    bunch of strings... How do you write a function that extracts each
    field, then returns both the string extracted and the position of the
    next field?

    Here are some ideas to solve that problem:

    string idea1( const char* in ) {
    string result;
    while ( *in && *in != '\t' )
    result.push_back( *in );
    return result;
    }

    The above function extracts the field, but the calling function must
    advance the input on its own, like this maybe:

    vector<string> fields;
    while ( *data ) {
    fields.push_back( idea1( data ) );
    data += fields.back().size() + 1;
    }

    However, if we make the parameter an in/out one, then we can do this:

    string idea2( const char*& inOut ) {
    string result;
    while ( *inOut && *inOut != '\t' )
    result.push_back( *inOut++ );
    ++inOut;
    return result;
    }

    Now the caller need not take care of pointer advancement on its own:

    vector<string> fields
    while ( *data ) {
    fields.push_back( idea2( data ) ); // data is modified by the call
    }

    Of course, in returning the string, note that the function has to build
    the string, then create a temporary copy of it to pass to the caller.
    Often the compiler can optimize the copy away (look up return value
    optimization (RVO)). However, if profiling shows this to be a
    bottleneck, we can use:

    void idea3( const char*& inOut, string& result ) {
    result = string();
    while ( *inOut && *inOut != '\t' )
    result.push_back( *inOut++ );
    ++inOut;
    }

    which would be used like so:

    vector<string> fields;
    while ( *data ) {
    string str;
    idea3( data, str );
    fields.push_back( str );
    }
    Daniel T., Dec 24, 2007
    #4
  5. Andre Kostur Guest

    wrote in news:1f7ca27b-5c4a-4458-a105-
    :

    > Hi,
    >
    > Recently i came across a functiion declaration as below
    >
    > void f(char *&a);
    >
    > Nowhere i have come across this style. I understand, passing by
    > reference is always better(to avoid copying), that too added with


    Not necessarily true. Probably true in the case of non-trivial types.
    char* isn't one of them. This function may want to be able to modify the
    pointer that it was passed.

    > const like
    >
    > void f(const std::string &a); //since it was char *a, i assume it to
    > be a string


    So now you've traded a ref-to-pointer with a potential construction of a
    temporary string object, with the attendant dynamic memory allocation
    (probably) and memory copy. And you've changed the semantics of the
    function.

    > This i came across in the book "Secrets of C++ Masters". Though this
    > book is old, i just wanted to know what it speaks of. I do not know
    > whether this is still accepted or was a part of pre-standard c++. I
    > tried a toy program with a function like this. It did compile fine and
    > executed successfully. As per the book, this style prevents pointer
    > copy(address copy) - is that true? atleast in c++. Pls advice.


    Implementation-dependant as to what potential savings it might have.
    Andre Kostur, Dec 24, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    10
    Views:
    695
    Chris Torek
    Feb 4, 2005
  2. jimjim
    Replies:
    16
    Views:
    835
    Jordan Abel
    Mar 28, 2006
  3. Replies:
    4
    Views:
    1,248
    Fred Zwarts
    Jul 2, 2009
  4. A
    Replies:
    7
    Views:
    632
  5. , India

    pointer to an array vs pointer to pointer

    , India, Sep 20, 2011, in forum: C Programming
    Replies:
    5
    Views:
    449
    James Kuyper
    Sep 23, 2011
Loading...

Share This Page