473,804 Members | 2,096 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.itemNum ber = 10;

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

return( newItem );
}
void PrintItem( Item iItemToPrint )
{
cout << iItemToPrint.in ternalItems[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 2638
On 13 nov, 14:46, ctj951 <chadsspameater em...@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.itemNu mber = 10;

* *newItem.intern alItems[ 0 ] = 1;
* *newItem.intern alItems[ 1 ] = 2;
* *newItem.intern alItems[ 2 ] = 3;
* *newItem.intern alItems[ 3 ] = 7;
* *newItem.intern alItems[ 4 ] = 9;

* *return( newItem );
* *}

void PrintItem( Item iItemToPrint )
* *{
* *cout << iItemToPrint.in ternalItems[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.itemNum ber = 10;

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

return( newItem );
}
void PrintItem( Item iItemToPrint )
{
cout << iItemToPrint.in ternalItems[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(dee p 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 <chadsspameater em...@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.itemNu mber = 10;
* *newItem.intern alItems[ 0 ] = 1;
* *newItem.intern alItems[ 1 ] = 2;
* *newItem.intern alItems[ 2 ] = 3;
* *newItem.intern alItems[ 3 ] = 7;
* *newItem.intern alItems[ 4 ] = 9;
* *return( newItem );
* *}
void PrintItem( Item iItemToPrint )
* *{
* *cout << iItemToPrint.in ternalItems[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
3971
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
4207
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 22 of section 6.2.5. If so, that seems to make it difficult to allocate such structures, because sizeof() is not allowed on incomplete types (paragraph 1 of section 6.5.3.4). For instance, I've routinely done things like this: struct foo {...
1
2326
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" void main() {
3
2849
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; } *arrays; It's supposed to be a structure containing an array of chars, an array of ints and an int. I declare functions like this : arrays *parseline(char *line, int N)
19
3158
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 than in .Net environment, under VS 2005 Beta 2. Does anyone have any idea whether this will be addressed in the final release? Thanks, Tomasz
30
3362
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
3170
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 thorughout the course the programs use so it will need to
6
2813
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 }, {"JProfStopProfiling", JProfStopProfiling,
5
2682
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 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...
0
9595
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10600
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10354
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9175
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7642
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4313
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3835
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3002
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.