5

If I have a struct like so:

struct S
{
    int arr[10];
    int x;
}

If I have a function

void some_fun( struct S myS );

Will the array be copied correctly when I pass a struct S to that function or do I have to change the function to:

void some_fun( struct S * myS ); ?

I have tested this (and it works without a pointer) but I am a bit unsecure about C/C++, sometimes it works even though it is wrong.

Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208

5 Answers5

6

Will the array be copied correctly when I pass a struct S to that function

Yes, it will be copied.

do I have to change the function to: void some_fun( struct S * myS )

You may want to do that for three reasons:

  • You want to avoid copying - passing a pointer is cheaper
  • You want to make changes to the struct inside some_fun - pointers let you do that; passing by value does not
  • You want to reduce the risk of stack overflow - if the array is large, passing by value may overflow the stack. Passing by pointer greatly reduces this risk.

EDIT : Since this is C++, you have an option of passing the struct by reference:

void some_fun( S& myS ) // struct keyword is optional in C++

or

void some_fun( const S& myS ) // Do this if you do not plan on modifying myS

Passing by reference avoids copying in a way similar to passing by pointer. Unlike passing by pointer, passing by reference indicates to the reader that the struct passed in references some place in memory, while pointers give you an option of passing a NULL.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

If you pass by value, as you are currently doing, your array will be copied by the default copy constructor (see e.g. here: How are C++ array members handled in copy control functions?).

If you decide to pass by pointer, only the pointer to the same struct will be passed into it - so your array will not be copied, any change you do to the array (or any other member of the struct) will reflect back to the object you access outside of the function.

Ultimately, it boils down to these things to decide which approach to choose:

  • Do you need a copy in the function? If not, passing by const reference would be better
  • Do you need to modify the exact same struct you have outside the function? Then use the pointer approach (or a reference).
  • Do you need a copy in the function, because you want to modify the struct in the function, but those changes should not reflect back to the calling place? Then use the pass-by-value approach you are currently using!
Community
  • 1
  • 1
codeling
  • 11,056
  • 4
  • 42
  • 71
0

You have 3 options:

Locally copied version:

void some_fun( struct S myS );

Pointer version:

 void some_fun( struct S* myS );

Reference version:

void some_fun( struct S& myS );

In the latter 2, changes made to myS will be persistent (affecting original instance). In the first case, you'll have a local copy.

  • You don't have to write `struct` everywhere in C++. Also, there are options what pass `const` references or pointers to `const. – juanchopanza Aug 20 '13 at 14:51
0

It will pass a copy of your data structure. This is fine unless you want some_fun to alter the actual data structure you're passing in, then you need a pointer or refrence.

Paul Evans
  • 27,315
  • 3
  • 37
  • 54
0

As you put an array with a const boundary, it's no more decided on runtime how big the structure will be. So in you'r case the Array is part of the structure, and not stored on a different place. so simply: Yes it will be parsed to the function!

BenMorel
  • 34,448
  • 50
  • 182
  • 322
dhein
  • 6,431
  • 4
  • 42
  • 74