Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Returning an array inside a structure that was allocated in afunction

Reply
Thread Tools

Returning an array inside a structure that was allocated in afunction

 
 
ctj951
Guest
Posts: n/a
 
      11-13-2008
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.
 
Reply With Quote
 
 
 
 
Leandro Melo
Guest
Posts: n/a
 
      11-13-2008
On 13 nov, 14:46, ctj951 <chadsspameaterem...@yahoo.com> 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
 
Reply With Quote
 
 
 
 
Bo Persson
Guest
Posts: n/a
 
      11-13-2008
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


 
Reply With Quote
 
mail.dsp@gmail.com
Guest
Posts: n/a
 
      11-14-2008
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
 
Reply With Quote
 
Leandro Melo
Guest
Posts: n/a
 
      11-14-2008
On 13 nov, 15:02, Leandro Melo <ltcm...@gmail.com> wrote:
> On 13 nov, 14:46, ctj951 <chadsspameaterem...@yahoo.com> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
Returning an array inside a structure that was allocated within afunction ctj951 C Programming 4 11-14-2008 04:15 AM
Accessing Individual rows from a multidimensional array passed to afunction ...vagrahb C++ 1 05-06-2008 05:44 PM
RE:"private" variables a.k.a. name mangling (WAS: What is print? Afunction?) Jeremy Bowers Python 2 01-25-2005 12:38 PM
Dynamically Allocated Memory vs. Statically allocated Memory csnerd@gmail.com C++ 5 12-09-2004 01:44 AM



Advertisments