Returning an array inside a structure that was allocated in afunction

Discussion in 'C++' started by ctj951, Nov 13, 2008.

  1. ctj951

    ctj951 Guest

    I have a very specific question about a language issue that I was
    hoping to get an answer to. If you allocate a structure that contains
    an array as a local variable inside a function and return that
    structure, is this valid?

    As shown in the code below I am allocating the structure in the
    function and then returning the structure. I know if the structure
    contained only simple types (int, float) this will work without
    problems as you are getting a copy of those items returned from the
    function. But I'm wondering with an array which is being returned
    from the function as part of the structure is a pointer to the local
    variable or perhaps a copy of that array (as it would be for simple
    types). I think we might be getting a pointer returned but I'm not
    sure.

    #include <iostream>
    using namespace std;

    struct Item
    {
    int itemNumber;
    int internalItems[5];
    };


    Item CreateItem()
    {
    Item newItem;

    newItem.itemNumber = 10;

    newItem.internalItems[ 0 ] = 1;
    newItem.internalItems[ 1 ] = 2;
    newItem.internalItems[ 2 ] = 3;
    newItem.internalItems[ 3 ] = 7;
    newItem.internalItems[ 4 ] = 9;

    return( newItem );
    }


    void PrintItem( Item iItemToPrint )
    {
    cout << iItemToPrint.internalItems[0];
    }


    int main ()
    {
    Item testItem = CreateItem();

    PrintItem( testItem );

    return 0;
    }

    This is a specific question about a specific language issue. Thank
    You.
    ctj951, Nov 13, 2008
    #1
    1. Advertising

  2. ctj951

    Leandro Melo Guest

    On 13 nov, 14:46, ctj951 <> wrote:
    > I have a very specific question about a language issue that I was
    > hoping to get an answer to.  If you allocate a structure that contains
    > an array as a local variable inside a function and return that
    > structure, is this valid?
    >
    > As shown in the code below I am allocating the structure in the
    > function and then returning the structure.  I know if the structure
    > contained only simple types (int, float) this will work without
    > problems as you are getting a copy of those items returned from the
    > function.  But I'm wondering with an array which is being returned
    > from the function as part of the structure is a pointer to the local
    > variable or perhaps a copy of that array (as it would be for simple
    > types).  I think we might be getting a pointer returned but I'm not
    > sure.
    >
    > #include <iostream>
    > using namespace std;
    >
    > struct Item
    >    {
    >    int itemNumber;
    >    int internalItems[5];
    >    };
    >
    > Item CreateItem()
    >    {
    >    Item newItem;
    >
    >    newItem.itemNumber = 10;
    >
    >    newItem.internalItems[ 0 ] = 1;
    >    newItem.internalItems[ 1 ] = 2;
    >    newItem.internalItems[ 2 ] = 3;
    >    newItem.internalItems[ 3 ] = 7;
    >    newItem.internalItems[ 4 ] = 9;
    >
    >    return( newItem );
    >    }
    >
    > void PrintItem( Item iItemToPrint )
    >    {
    >    cout << iItemToPrint.internalItems[0];
    >    }
    >
    > int main ()
    >    {
    >    Item testItem = CreateItem();
    >
    >    PrintItem( testItem );
    >
    >    return 0;
    >    }
    >
    > This is a specific question about a specific language issue.  Thank
    > You.




    This is one of the reasons copy constructors exist. ;) Think about
    which semantics you would like for a particular class (deep copy,
    shallow copy) and write a copy constructor accordingly.

    --
    Leandro T. C. Melo
    Leandro Melo, Nov 13, 2008
    #2
    1. Advertising

  3. ctj951

    Bo Persson Guest

    Re: Returning an array inside a structure that was allocated in a function

    ctj951 wrote:
    > I have a very specific question about a language issue that I was
    > hoping to get an answer to. If you allocate a structure that
    > contains an array as a local variable inside a function and return
    > that structure, is this valid?
    >
    > As shown in the code below I am allocating the structure in the
    > function and then returning the structure. I know if the structure
    > contained only simple types (int, float) this will work without
    > problems as you are getting a copy of those items returned from the
    > function. But I'm wondering with an array which is being returned
    > from the function as part of the structure is a pointer to the local
    > variable or perhaps a copy of that array (as it would be for simple
    > types). I think we might be getting a pointer returned but I'm not
    > sure.
    >
    > #include <iostream>
    > using namespace std;
    >
    > struct Item
    > {
    > int itemNumber;
    > int internalItems[5];
    > };
    >
    >
    > Item CreateItem()
    > {
    > Item newItem;
    >
    > newItem.itemNumber = 10;
    >
    > newItem.internalItems[ 0 ] = 1;
    > newItem.internalItems[ 1 ] = 2;
    > newItem.internalItems[ 2 ] = 3;
    > newItem.internalItems[ 3 ] = 7;
    > newItem.internalItems[ 4 ] = 9;
    >
    > return( newItem );
    > }
    >
    >
    > void PrintItem( Item iItemToPrint )
    > {
    > cout << iItemToPrint.internalItems[0];
    > }
    >
    >
    > int main ()
    > {
    > Item testItem = CreateItem();
    >
    > PrintItem( testItem );
    >
    > return 0;
    > }
    >
    > This is a specific question about a specific language issue. Thank
    > You.


    This is quite ok. You are returning a struct, and all its members will
    be copied. There are no pointers involved.


    Bo Persson
    Bo Persson, Nov 13, 2008
    #3
  4. ctj951

    Guest

    No need to think about it. Since in structure you've allocated memory
    at compile time therefore default copy constructor will be invoked and
    it will copy all the member of structure properly. Even it will work
    in case of assignment too. But in case of run time allocation you'll
    have to define your copy constructor(deep copy) and overload
    assignment operator for proper functionality.

    --
    Daya S. Prasad
    , Nov 14, 2008
    #4
  5. ctj951

    Leandro Melo Guest

    On 13 nov, 15:02, Leandro Melo <> wrote:
    > On 13 nov, 14:46, ctj951 <> wrote:
    >
    >
    >
    > > I have a very specific question about a language issue that I was
    > > hoping to get an answer to.  If you allocate a structure that contains
    > > an array as a local variable inside a function and return that
    > > structure, is this valid?

    >
    > > As shown in the code below I am allocating the structure in the
    > > function and then returning the structure.  I know if the structure
    > > contained only simple types (int, float) this will work without
    > > problems as you are getting a copy of those items returned from the
    > > function.  But I'm wondering with an array which is being returned
    > > from the function as part of the structure is a pointer to the local
    > > variable or perhaps a copy of that array (as it would be for simple
    > > types).  I think we might be getting a pointer returned but I'm not
    > > sure.

    >
    > > #include <iostream>
    > > using namespace std;

    >
    > > struct Item
    > >    {
    > >    int itemNumber;
    > >    int internalItems[5];
    > >    };

    >
    > > Item CreateItem()
    > >    {
    > >    Item newItem;

    >
    > >    newItem.itemNumber = 10;

    >
    > >    newItem.internalItems[ 0 ] = 1;
    > >    newItem.internalItems[ 1 ] = 2;
    > >    newItem.internalItems[ 2 ] = 3;
    > >    newItem.internalItems[ 3 ] = 7;
    > >    newItem.internalItems[ 4 ] = 9;

    >
    > >    return( newItem );
    > >    }

    >
    > > void PrintItem( Item iItemToPrint )
    > >    {
    > >    cout << iItemToPrint.internalItems[0];
    > >    }

    >
    > > int main ()
    > >    {
    > >    Item testItem = CreateItem();

    >
    > >    PrintItem( testItem );

    >
    > >    return 0;
    > >    }

    >
    > > This is a specific question about a specific language issue.  Thank
    > > You.

    >
    > This is one of the reasons copy constructors exist. ;) Think about
    > which semantics you would like for a particular class (deep copy,
    > shallow copy) and write a copy constructor accordingly.


    Hmm... your array is statically allocated. No worry then.

    --
    Leandro T. C. Melo
    Leandro Melo, Nov 14, 2008
    #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:
    5
    Views:
    601
    Matt Wharton
    Dec 9, 2004
  2. Jeremy Bowers
    Replies:
    2
    Views:
    276
    Sion Arrowsmith
    Jan 25, 2005
  3. ...vagrahb
    Replies:
    1
    Views:
    292
    Vidar Hasfjord
    May 6, 2008
  4. ctj951
    Replies:
    4
    Views:
    271
    Keith Thompson
    Nov 14, 2008
  5. Replies:
    7
    Views:
    170
    James Kuyper
    Aug 27, 2013
Loading...

Share This Page