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
1 2 3 4
int func (int a){return a;}
//...
int x=5;
x=func(x);
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)
1 2 3 4 5 6 7 8 9 10 11
#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;
res.Array[0]=3;res.Array[1]=5;
//call function and pass struct
res=function(res);
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)
IS THERE ANY OTHER WAY?
Thank you for your time reading
P.S.
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?
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.
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.
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 valuebut 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 ^_^
P.S.
Sorry for 'Methods' we call them like that also in Greece
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.