How can i pass an entire Array into a function parameter by value? (no pointer/ref pass)

C/C++ Language in g++.I want to pass an entire Array (2D or 1D) into a function by value without passing its adress.And return it in the same way.Like we do with variables
int func (int a){return a;}
int x=5;

So the function will have a local entire copy of the original Array given by the caller

Until now i create and pass it inside a structure by Deep Copy but by this way I end having a global value in objects methods (bad situation)

#define SIZE 100
struct Array{ int Array[SIZE] };

//declare function with return type struct Array
struct Array function (struct Array result )
{return result;}
//in main now
struct Array res;
//call function and pass struct

It works perfectly and since the array its static, its passed by deep copy. But I get having this struct as a global.

Dont want that for my classes(its for a multithread concurent api). Tried creating the same struct in each class but then i couldnt pass between methods from diferent classes (logical)


Thank you for your time reading


I noticed that Im able to create local obgects in diferent methods with identical names. THey dont conflict. So its like a typedef? If objects inside scopes are local then its ok. I wont have conflicts?
Last edited on
In your case I would just use std::array - it will do all the work for you.

As for local objects with same name - yes, you can create a variable with same name in a different (maybe nested) scope. The variable in the outer scope will be hidden (same works for global scope). But to be honest, I don't think it is a good practice to have two variables with the same name - why would you need that? If it is a variable with a different purpose - why not naming it accordingly. If it has the same purpose - why do you need another variable?

One more thing - in C++ you don't need to write struct Array when declaring a variable or a function, as you would have to in C; simple Array f(Array result) {...} will do the job.
Last edited on
closed account (zb0S216C)
There is no way of doing this, except for the solution you've posted. The other solution is to create an array local to function X() and copy the elements from the source array to the local array. This has its own overhead with copying, especially when the elements of the source array are quite large. Though, your solution has this overhead, too.

imakaia wrote:
"I noticed that Im able to create local obgects in diferent methods with identical names. THey dont conflict. So its like a typedef?"

First, C++ has functions & member functions. No methods. In simple terms, identical identifiers in different functions are not type-definitions. They are in fact completely different from each other. Because a function has its own scope, each identifier is within its own scope.

Last edited on
I came up too with copying arrays, but stillI would get that instant of common access from diferent threads until is done. Dont want that

Of course as sed, its still a copy with passing a structure by value but now its done from the hardware in lower level.
There is no risk for race conditions - from concurent threads - downe there wright? Atomicity of operations its granted no?

Ahh I can miss struct. ^_^ (the fb img)
Thank you so much my declarations were geting so stringy ^_^

Sorry for 'Methods' we call them like that also in Greece
Last edited on
Atomicity of operations its granted no?

Which operations? Normally, very few of C++ commands may be considered atomic. It's very hard to say if your code will be thread safe without seeing some of it.

Something tells me that you will need to ensure thread safety with synchronization primitives (e.g. mutexes). You may also try using Intel TBB's concurrent_vector which allows access to the same vector from multiple threads.
Here is some example of it.. i learnt from there so i think that may be useful to you..
Topic archived. No new replies allowed.