473,396 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Returning an array inside a structure that was allocated in afunction

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.
Nov 13 '08 #1
4 2615
On 13 nov, 14:46, ctj951 <chadsspameaterem...@yahoo.comwrote:
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
Nov 13 '08 #2
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
Nov 13 '08 #3
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 '08 #4
On 13 nov, 15:02, Leandro Melo <ltcm...@gmail.comwrote:
On 13 nov, 14:46, ctj951 <chadsspameaterem...@yahoo.comwrote:
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
Nov 14 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: pvinodhkumar | last post by:
The number of elemets of the array, the array bound must be constant expression?Why is this restriction? Vinodh
6
by: Eric Smith | last post by:
Is a structure containing an incomplete array as its last element (per paragraph 2 of section 6.7.2.1 of ISO/IEC 9899:1999 (E)) itself an incomplete type? That appears to be indicated by paragraph...
1
by: Guha | last post by:
I have a problem with returning a 2D array using a function which is called in main(). The piece of the code is given below. This is a test code only. #include"stdio.h" #include"alloc.h" ...
3
by: Michel Rouzic | last post by:
It's the first time I try using structs, and I'm getting confused with it and can't make it work properly I firstly define the structure by this : typedef struct { char *l1; int *l2; int Nval; }...
19
by: Tom Jastrzebski | last post by:
Hello, I was just testing VB.Net on Framework.Net 2.0 performance when I run into the this problem. This trivial code attached below executed hundreds, if not thousand times faster in VB 6.0...
30
by: questions? | last post by:
say I have a structure which have an array inside. e.g. struct random_struct{ char name; int month; } if the array is not intialized by me, in a sense after I allocated a
7
by: Sam | last post by:
Hello I have a structure called Company. struct Company { char *employee; char *employee_address; }; I want to build an array of this structure but the number of employees will change...
6
by: silverburgh.meryl | last post by:
Hi, In one A.cpp file, I have defined a static array of JSFunctionSpec, like this: static JSFunctionSpec JProfFunctions = { {"JProfStartProfiling", JProfStartProfiling, 0, 0, 0 },...
5
by: ctj951 | last post by:
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.