C ++: Passing a structure containing an array as arg, do I need to pass as a pointer?

If I have a structure like this:

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 the S structure of this function or will I have to change the function:

 void some_fun( struct S * myS ); ? 

I tested this (and it works without a pointer), but I'm a little unsure of C / C ++, sometimes it works, even if it is wrong.

+4
source share
5 answers

Will the array be copied correctly when I pass the S structure of this function

Yes, it will be copied.

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

You can do this for three reasons:

  • You want to avoid copying - pointer transfer is cheaper
  • You want to make changes to the structure inside some_fun - pointers allow you to do this; transfer by value not
  • You want to reduce the risk - if the array is large, then passing by value can overflow the stack. Passing by pointer greatly reduces this risk.

EDIT:. Since this is C ++, you can pass a 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 manner similar to passing by a pointer. Unlike passing by pointer, passing by reference indicates to the reader that the struct passed some place in memory in the link, while pointers give you the option to pass NULL .

+6
source

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

If you decide to follow the pointer, only a pointer to the same structure will be passed into it, so your array will not be copied, any change you make to the array (or any other member of the structure) will reflect back to the object, to which you are accessing outside the function.

Ultimately, it comes down to these things to decide which approach to take:

  • Do you need a copy in function? If not, passing a constant will be better
  • Do you need to change the same structure as outside the function? Then use a pointer approach (or link).
  • Do you need a copy in the function because you want to change the structure in the function, but these changes should not be reflected in the calling place? Then use the default approach to use!
+1
source

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 permanent (affecting the original instance). In the first case, you will have a local copy.

0
source

It will transfer a copy of your data structure. This is normal, if you do not want some_fun change the actual data structure you are passing in, then you need a pointer or refrence.

0
source

As you place an array with a constant border, it no longer determines the runtime of how large the structure is. Therefore, in your case, the array is part of the structure and is not stored elsewhere. so simple: Yes, he will deal with the function!

0
source

Source: https://habr.com/ru/post/1498013/


All Articles