Returning an array inside a structure that was allocated within afunction

Discussion in 'C Programming' 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 <stdio.h>

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

    struct Item CreateItem()
    {
    struct 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( struct Item iItemToPrint )
    {
    printf( "%d", iItemToPrint.internalItems[0] );
    }

    int main ()
    {
    struct 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. Re: Returning an array inside a structure that was allocated within a function

    ctj951 <> writes:

    > 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?


    Yes. In fact, this is really the only way to return an array from a
    function.

    > 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.


    Nope, the entire structure, including the array, gets copied (*). So you're
    on firm ground here.

    (*) Conceptually speaking. In practice, the compiler can do anything
    that has the same effect. It might be able to merge the two copies into
    one.

    > #include <stdio.h>
    >
    > struct Item
    > {
    > int itemNumber;
    > int internalItems[5];
    > };
    >
    > struct Item CreateItem()
    > {
    > struct 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( struct Item iItemToPrint )
    > {
    > printf( "%d", iItemToPrint.internalItems[0] );
    > }
    >
    > int main ()
    > {
    > struct Item testItem = CreateItem();
    > PrintItem( testItem );
    > return 0;
    > }
    >
    > This is a specific question about a specific language issue. Thank
    > You.
     
    Nate Eldredge, Nov 13, 2008
    #2
    1. Advertising

  3. ctj951

    James Kuyper Guest

    Re: Returning an array inside a structure that was allocated withina 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?


    Yes.

    > 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.


    No. The array is an array, not a pointer. It takes up actual space in
    the local array, and gets copied along with the rest of the struct when
    the 'return' statement is executed.

    > #include <stdio.h>
    >
    > struct Item
    > {
    > int itemNumber;
    > int internalItems[5];
    > };
    >
    > struct Item CreateItem()
    > {
    > struct 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( struct Item iItemToPrint )
    > {
    > printf( "%d", iItemToPrint.internalItems[0] );
    > }
    >
    > int main ()
    > {
    > struct Item testItem = CreateItem();


    The 'return' statement in CreateItem() causes the local new_item object
    to be copied to the testItem. The contents of the array get copies along
    with the rest of the struct.

    > PrintItem( testItem );
    > return 0;
    > }
     
    James Kuyper, Nov 13, 2008
    #3
  4. ctj951

    S M Ryan Guest

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

    In article <>,
    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?


    An array declared inside a struct has the same scope as the entire struct.
    Anywhere you can use the struct, the array also exists.

    --
    I'm not even supposed to be here today.

    I ASSURE YOU WE'RE OPEN!
     
    S M Ryan, Nov 14, 2008
    #4
  5. Re: Returning an array inside a structure that was allocated within a function

    ctj951 <> writes:
    > 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 <stdio.h>
    >
    > struct Item
    > {
    > int itemNumber;
    > int internalItems[5];
    > };
    >
    > struct Item CreateItem()
    > {
    > struct 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( struct Item iItemToPrint )
    > {
    > printf( "%d", iItemToPrint.internalItems[0] );
    > }
    >
    > int main ()
    > {
    > struct Item testItem = CreateItem();
    > PrintItem( testItem );
    > return 0;
    > }


    This comes close to a topic that we had a lengthy discussion about
    just recently.

    There's no problem with the code you posted.

    <DIGRESSION>
    Well, there are a couple of minor things, but they're not relevant to
    your question. The way to declare function with no parameters is
    "(void)"; "()" indicates an unspecified number and type of parameters,
    which is legal but IMHO not as good. <OT>In C++, empty parentheses do
    indicate that the function has no parameters.</OT> The parentheses on
    the return statement are unnecessary (but harmless). And you should
    print a new-line at the end of your output.
    </DIGRESSION>

    But there's an issue that you could run into.

    As others have pointed out, a function returning a structure returns
    it by value; the result is a single value of type struct ITem,
    consisting of the values of the itemNumber and internalItems members.
    There's no problem returning such a value from a function, assigning
    it to an object or using to initialize an object, or passing it to
    another function.

    But if you tried to index the array member of the result directly, you
    could run into problems.

    For example, if you write:

    printf("%d\n", CreateItem().internalItems[0]);

    then something odd happens. CreateItem().internalItems is an
    expression of array type. In most contexts, including this one, such
    an expression is implicitly converted to a pointer to the first
    element of the array object. The [0] indexing operator then extracts
    the 0th element of that array object.

    "What array object?", I hear you cry. Good question. The array is a
    member of a struct *value*; there is no array object. But the C
    standard says that there has to be an object. This is a hole in the C
    standard, something that I suspect the authors just didn't think of at
    the time. Different compilers may handle this differently; some
    probably do handle it in such a way that it Just Works, but others
    might not.

    The workaround is simple: Don't directly index an array member of a
    function result. Instead, save the returns struct value in an object
    and use that to do the indexing, and you won't have to worry about
    this issue.

    The preliminary first draft of the C201X standard adds wording that
    says an object of "temporary lifetime" is created in this case. Grab
    a copy of n1336.pdf and see section 6.2.4 if you're really interested.

    Incidentally, the form of your code makes me suspect that you're
    actually working in C++. The difference doesn't affect the code you
    posted (except perhaps for the meaning of the "()" in the function
    declarations), but I think C++ handles the indexing glitch differently
    (as I recall, a value returned from a function is treated as a
    temporary object). If you care, ask in comp.lang.c++.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, 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:
    623
    Matt Wharton
    Dec 9, 2004
  2. Jeremy Bowers
    Replies:
    2
    Views:
    284
    Sion Arrowsmith
    Jan 25, 2005
  3. ...vagrahb
    Replies:
    1
    Views:
    298
    Vidar Hasfjord
    May 6, 2008
  4. ctj951
    Replies:
    4
    Views:
    307
    Leandro Melo
    Nov 14, 2008
  5. Replies:
    7
    Views:
    184
    James Kuyper
    Aug 27, 2013
Loading...

Share This Page