[SOLVED] How can i pass an entire Array into a function parameter by value? (no pointer pass)
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How can i pass an entire Array into a function parameter by value? (no pointer pass)
C/C++ Language.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
PHP Code:
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 a pass it inside a structure by Deep Copy but by this way I end having a global value in omjects methods (bad situation)
PHP Code:
#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 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
EDIT:PROVIDE SOLUTION SOLUTION
As from post #7
This is the only way to achieve this. Otherwise you could pass the pointer and copy manually to another local array BUT you would still get that instant of common access to the same memory for diferent concurent threads.
Also in cpp you dont have to wright struct in declarations like c
struct Array; Array f(Array result) {...} will be ok
Last edited by esgol; 07-30-2012 at 03:41 AM.
Reason: Provided 3rd party Solution after bad aceptance to not leave a Zombie Thread
THe code is intended for oncurency. It mus be pure functional meaning no outer dependencies with what is passed nor inner ones with what is returned.
Passing by reference its stil connected with the original like a pointer
So i need to pass by value inside a struct. But i get to use thestruct like a global value in all classes. Is there a way to avoid this
I see no basis in your statements.
I.e. I do not necessarily see a problem if something is passed by reference in the context of concurrency.
Or, to put it differently, if you pass something by value rather than by pointer/reference, you create a copy of the thing being passed. So, I do not understand why you need to create a copy in the first place.
Sigh That is personally unquestionable. Thanks anyway for the councils
Others dont forget I asked how to pass by value (copy) an Array inside an Array.
Are structures/classes the only way?
I note that when i create local obgects of teh structs inside methods they re not global even if they have the same name.
Surely they wont be miscified? Then that would be ok to use structs
Sigh That is personally unquestionable. Thanks anyway for the councils
Others dont forget I asked how to pass by value (copy) an Array inside an Array.
Are structures/classes the only way?
I note that when i create local obgects of teh structs inside methods they re not global even if they have the same name.
Surely they wont be miscified? Then that would be ok to use structs
I am not sure you understand what you're trying to do. I've been dealing with parallelism both as a HW (chip) designer and as a programmer, and I know data copies are not always necessary (but sometimes they are).
I really don't understand:
1) why you insist on creating a copy of data to be passed to function (but you may have a need, you haven't explained it yet);
2) even if you do need a copy, why you insist on creating it just before the actual call (by passing the data by value) and not at some later moment of time/code execution.
Not sure why passing by reference is not preferred in your case, ... how about a memcpy() of a contagious memory? Of course you need to make sure your receiving structure is of the same alignment and right size.
Last edited by Jerry Mcguire; 07-29-2012 at 07:56 PM.
I ve explained Its gonna be a concurent aplication.
If you have any lack of knoledge there you have to know that functions have to be pure functional with their mathematical mean, means no pointers-refs passed or returned.
So you need always by value and deep copies.
Ahh this is so analytical dwhitney67 Thanks very much
I ve explained Its gonna be a concurent aplication.
If you have any lack of knoledge there you have to know that functions have to be pure functional with their mathematical mean, means no pointers-refs passed or returned.
So you need always by value and deep copies.
Ahh this is so analytical dwhitney67 Thanks very much
Your explanation does not make any sense.
I.e. concurrent applications may well work with pointers.
It looks like you have profound lack of knowledge. And you just make axiomatic statements: "functions have to be pure functional", and you think that we should obediently accept your religious beliefs.
If you want to show that you know something, prove that a pointer is harmful in your application/scenario. But remember that a lot of CPUs have a register called SP - "Stack Pointer", and those CPUs are somehow capable of executing concurrent applications.
I.e. concurrent applications may well work with pointers.
It looks like you have profound lack of knowledge. And you just make axiomatic statements: "functions have to be pure functional", and you think that we should obediently accept your religious beliefs.
If you want to show that you know something, prove that a pointer is harmful in your application/scenario. But remember that a lot of CPUs have a register called SP - "Stack Pointer", and those CPUs are somehow capable of executing concurrent applications.
Sergei... I agree with your comments, however I think you scared the OP away. He marked the thread as solved. :-)
Writing data pointed to by pointer may be a problem if two or more separate processes try to write to the same area.
Yes this is what i ment by conncurent safe. Its the obvious concurency problem.
Yes there are ways to programm concurent with pointers, but since I have only theoritical knowledge of the issue, i'll develop the API as Serial 1st trying to make it as object oriented and functional as possible keeping always in mind how the obgjects and methods, of a Multithread version should be, trying in the same time to secure the atomicity of operations as much as possible avoifing throwing pointers here and there.
If i was sending the arrays pointers then i should use mutexes or make free-lock programming designing the structure of the code entirely different.
For start trying having the code totaly obgect oriented and pure functional its a good start i think :S
But as i repeat, i havent asked councils on this neither referared, JUST HOW TO PASS BY VALUE A DAMNED ARRAY not WHY I SHOULD or IF I SHOULD.
Thats what sergei did not understood thanks for the further interest however, most usually don't even care
P.S.
dwhitney wrote
Quote:
Sergei... I agree with your comments, however I think you scared the OP away. He marked the thread as solved. :-)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.