LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-29-2012, 02:13 PM   #1
esgol
Member
 
Registered: Jul 2012
Posts: 106
Blog Entries: 1

Rep: Reputation: Disabled
Question 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
 
Old 07-29-2012, 03:15 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by esgol View Post
C/C++ Language.I want to pass an entire Array (2D or 1D) into a function by value without passing its adress. ...
Why/what for ? I.e. what is bad with passing it by reference ?
 
Old 07-29-2012, 03:30 PM   #3
esgol
Member
 
Registered: Jul 2012
Posts: 106
Blog Entries: 1

Original Poster
Rep: Reputation: Disabled
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

Last edited by esgol; 07-29-2012 at 03:43 PM.
 
Old 07-29-2012, 04:04 PM   #4
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by esgol View Post
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.
 
Old 07-29-2012, 04:59 PM   #5
esgol
Member
 
Registered: Jul 2012
Posts: 106
Blog Entries: 1

Original Poster
Rep: Reputation: Disabled
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
 
Old 07-29-2012, 06:40 PM   #6
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by esgol View Post
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.
 
1 members found this post helpful.
Old 07-29-2012, 07:19 PM   #7
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,507

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
Quote:
Originally Posted by esgol View Post
C/C++ Language.
C and C++ are two DIFFERENT languages. Pick one.

As for your question, perhaps you should spend some time reading through this thread: http://stackoverflow.com/questions/1...reference-in-c
 
1 members found this post helpful.
Old 07-29-2012, 07:54 PM   #8
Jerry Mcguire
Member
 
Registered: Jul 2009
Location: Hong Kong SAR
Distribution: RedHat, Fedora
Posts: 136

Rep: Reputation: 17
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.
 
Old 07-29-2012, 07:58 PM   #9
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
The fundamental question still is: "why a copy of data is needed in the first place ?".

There is no sense in further coding until this question is answered.
 
Old 07-30-2012, 03:37 AM   #10
esgol
Member
 
Registered: Jul 2012
Posts: 106
Blog Entries: 1

Original Poster
Rep: Reputation: Disabled
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

Last edited by esgol; 07-30-2012 at 03:42 AM.
 
Old 07-30-2012, 04:41 AM   #11
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by esgol View Post
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.
 
1 members found this post helpful.
Old 07-30-2012, 05:07 AM   #12
gchen
Member
 
Registered: May 2012
Location: Beijing China
Distribution: Asianux
Posts: 56

Rep: Reputation: Disabled
Smile

I support what Sergei Steshenko said.

1st, esgol is truly very careful, and it is very good attitude (especially for C programmer in concurrency programming).

2nd, MAYBE with PHP, what esgol said is correct (sorry for I do not know PHP very much).

3rd, but in C (not C++, they are truly very different), I think what Sergei Steshenko said is truly correct,and truly helpful for you.

: )
 
Old 07-30-2012, 05:17 AM   #13
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
If we go into further details, reading data pointed to by pointer is no problem at all.

Writing data pointed to by pointer may be a problem if two or more separate processes try to write to the same area.

But none of this has been mentioned by the OP here.
 
Old 07-30-2012, 05:54 AM   #14
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,507

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
Quote:
Originally Posted by Sergei Steshenko View Post
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.
Sergei... I agree with your comments, however I think you scared the OP away. He marked the thread as solved. :-)
 
Old 07-30-2012, 06:38 AM   #15
esgol
Member
 
Registered: Jul 2012
Posts: 106
Blog Entries: 1

Original Poster
Rep: Reputation: Disabled
Quote:
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. :-)
Lol dwhitney67 ^_^
http://farm4.static.flickr.com/3148/...269be596e0.jpg

Last edited by esgol; 07-30-2012 at 06:40 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] bash how pass array as one of several parameters to a function porphyry5 Programming 5 06-14-2011 09:11 PM
[C] Pass pointer of struct to function by refrence PJani Programming 3 11-19-2008 12:49 AM
in C, how do I pass a pointer of an array of structures to a function? randomsel Programming 2 02-04-2008 06:23 PM
Can't pass 2d-array-class-pointer Bariton Programming 3 09-11-2007 08:30 AM
how to pass pointer of struct to function? jinxcat Programming 2 09-01-2005 09:29 AM


All times are GMT -5. The time now is 06:34 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration