Sending arrays from a program to another

i have two programs, the main program reads a list of files and them read the files one by one storing their numeric content in unidimensional matrices like this:

1
2
3

sscanf (x[j],"%f  %e  %e",&a[j],&b[j],&c[j]);


being a and b the arrays i need to send to the other program

the other program need to get this arrays, do its function and send back a new answer variable so the main program can print the awnsers in a file.

Im planing on compilating both programs, progMain.c and function.o in one.

I really serched for examples of this on google wich i tought wold be easy but found none, really need help.

Do both programs have a main routine, or could one of them be a function you call?

If both programs stand alone, you could use sockets to send the data; you could use shared memory, you could use pipes, you could use threads. It just depends on how you want to do it.
They both stand alone, i was hoping i could turn the secondary program into a function and do something like:

1
2
3
4
5
6
7
8
9

main{

...

function.o(a,b,...)

}


but i dont have any models, also i need to return a variable of the secundary that dont exist on the main, the awnser to those arrays.
I'd say use sockets. VERY simple to implement, pretty reliable (but i'd recommend using blocking/default calls on a separate thread).

Basically:
1
2
#include <winsock.h> //or winsock2.h
#pragma comment(lib,"winsock.lib") //or ws2_32.lib. Pardon me if this is the wrong lib i always use winsock2 


That's probably what might not be covered in the tuts online....Basically one of your programs would run as a "server" listening on a "port" you choose (set host to "localhost" or 127.0.0.1) and one would act as a "client" that connects to the "server" program on said port.
Then you can send() and recv() data between the two programs. so like:
send(socket,"ARRAY (array here)");
/////////OTHER PROGRAM:////////
recv(socket,Buf);
and you can do some other cool things with sockets :)
go google winsock2 tutorial for full code/syntax :)
closed account (zb0S216C)
The volatile[1] keyword maybe what you're looking for.

References:
[1]http://msdn.microsoft.com/en-us/library/12a04hfd(v=VS.100).aspx


Wazzak
Last edited on
Does this work on linux?

thanks for the help guys.
closed account (zb0S216C)
vtergolina wrote:
Does this work on linux? (sic)

I found this:

Wikipedia wrote:
Operations on volatile variables are not atomic, nor do they establish a proper happens-before relationship for threading. This is according to the relevant standards (C, C++, POSIX, WIN32), and this is the matter of fact for the vast majority of current implementations. The volatile keyword is basically worthless as a portable threading construct.

And this: http://kernel.org/doc/Documentation/volatile-considered-harmful.txt

Wazzak
Last edited on
vtergolina

What do you mean by: "Im planing on compilating both programs, progMain.c and function.o in one." ?

Andy

What do you mean by: "Im planing on compilating both programs, progMain.c and function.o in one." ?


My idea was to transform the function program into a function .o and declare it on the main program so later i could call it as a function that would do what need too be done to the arrays. Something like:

1
2
3
4
5
6
7
8
9
10
11
#stdio.h
#function.o

main{

....

function(arrayA, arrayB) 

....
}


So the program i am transforming into a function wold change its variables to be dependent of a main program, like:

1
2
3
4
5
6
7

void function{ float a, b, int ...) {

...

}


then i would compile both togheter so the executable would contain both.
It sounds like your talking about one program with two functions, each in their own source file.

#function.o is not valid c/c++

1
2
3
// function.h

extern void function( float a, float b );


1
2
3
4
5
6
7
8
9
10
11
// function.cpp

#include <stdlib.h>

#include "function.h"

void function( float a, float b ) {

...

}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
main.cpp

#include <stdio.h>

#include "function.h"

int main(){

....

    float a = 1.0f;
    float b = 2.0f;

    function(a, b); 

....

    return 0;
}


compile : main.cpp -> main.o
compile : function.cpp -> function.o
link : main.o + function.o + required libs -> executable


Last edited on
Yeah andy, thats it, but i'm using an unidimensional matrice format, a[i] and b[i] because they are arrays, does this method work on this kind of variables to?
Yes... just change the function signature to

void function(double a[], double b[]);

or

void function(double* a, double* b);

or whatever variable types you need (remembering to use const for in params).

Andy

P.S. Are you familiar with std::vector ???
P.S. Are you familiar with std::vector ???


No, i've learned basics C (physics student) and i'm using this programs for scientific purpouses, but if you think would be useful for me i going to take a look, i'm trying to learn the most i can of C++.

I have a last question to, there is a variable on the function that is the awnser to the arrays of the main program, is there a way to send this variable from the function to the main program since it doesn't excists on the main program?
This variable also is an unidimensional matrice array.

I'm using gcc as compiler.
Last edited on
So my function.c stood like this:

1
2
3
4
5
6
7
8
9
10
#include <stdio.h>
#include <stdlib.h>
#include "function.h"

void filtro_solo( float Sx[], float h[], float Cor[][], int PontosSpec, int v )
{
int ...
float ...
...
}


tried to compile it like this:

% gcc -c filtro_solo.c

and obtained this error:

function.h:1: error: expected ‘;’, ‘,’ or ‘)’ before ‘float’
filtro_solo.c:14: error: array type has incomplete element type

Any idea? Also, must i turn the programs into .o to?
Last edited on
The error message does not help as you don't quote the relevant code (what's on line 14?)

But... float Cor[][] is not a valid for a parameter definition. Only one dimension of a multi-dimension array can be left undefined.

For example, both these function definitions are OK

1
2
3
void filtro_solo( float Sx[], float h[], float Cor[][2], int PontosSpec, int v )

void filtro_solo( float Sx[], float h[], float Cor[2][2], int PontosSpec, int v )


And for a 3 dimensional array

void filtro_solo( float Sx[], float h[], float Cor[][3][2], int PontosSpec, int v )

If you are passing an array, it is good practice to pass the dimensions, too.

And:
- yes, you need to turn function.c into function.o
- C++ would allow you to pass an Array object for your Cor parameter
- you might want to consider using a scientific library, if you are not expected to write your code from the ground up

Andy

P.S.
Three possible scientific libraries (I have not used them myself)
http://www.oonumerics.org/blitz/
http://arma.sourceforge.net/
http://www.gnu.org/software/gsl/
(there are others: a Physic forum might be a better place for advice about which is more suitabke for your needs?)
Last edited on
The error message does not help as you don't quote the relevant code (what's on line 14?)


Sorry, the line 14 is this:

void filtro_solo( float Sx[], float h[], float Cor[][], int PontosSpec, int v )

I'm really grateful for your help andy, i got the basics for this process from numerical recipes, but it was to complicated i didnt understood how it really works.

I'm planing on geting better and learning new languages, you got any advice on sites or books for that propose? Also on useful languages.

Again, thanks a lot.
OK, then the error on line 14 is the same as the one I described above (that you can't just use Cor[][]).

For book recommendations, see:
http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list
(from the books I do recognize, the selection seems good)

You could also search this site for other discussions about books?

Aside from this sites tutotial:
http://www.cplusplus.com/doc/tutorial/

See:
http://www.learncpp.com/
http://www.cprogramming.com/tutorial.html

You might also find the C++ FAQ useful:
http://www.parashift.com/c++-faq-lite/

And at some point you should read Stroustrup's FAQ
http://www2.research.att.com/~bs/bs_faq.html
Regarding programming languages, the choice really depends on what you want to do.

For assorted opinions see:
http://www.daniweb.com/software-development/computer-science/threads/277152

Googling for "best programming language" finds assorted sites answering this kind of question. For example, best language for web development, best language for scientific computing, ...

One which sound interesting is: I was goinf to suggest you ask some physicists!

"Which programming language is best for Scientists/Modeling Physical World"
http://www.physicsforums.com/showthread.php?t=332618

I use C++ for most of my work, but Python for small utility programs. I also use C#, but it solves a similar set of problems to C++. It's just easier to use, due to its memory management. But it can be slower for algorithmic code.
Last edited on
Topic archived. No new replies allowed.