Is it possible to send a pointer to function via socket? [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have two processes of the same program, possibly running on two different machines.
I'd like the process P2 to receive a function pointer from process P1 via socket.
Is is possible? Is it a good practice?
EDIT: more interesting would be to send the code of the function too, but I'm skeptic about this. Would it be possible?

You can send a function pointer from one process to another, the same way you can send a pointer to some other object.
The problem is that the pointer may not actually point to the function as it exists in the target process. Especially if the OS is protecting itself with things like ASLR.
You could also send the code across, provided you had some way of figuring out where it ended, and that it was position independent code, and that your environment allowed you to write arbitrary data to memory and then call it.
But, to be honest, there are better ways to achieve what you seem to want, such as the use of RPC (remote procedure calls), in a more portable manner.

Related

How to detect and communicate with another process under Linux? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have a program 'P' and P is executed in terminal A. Let's call it process A. While process A is running, terminal B is opened and executes P as process B.
How can I make process A find process B and exchange data with each other? Someone told me to implement it with MPI but I haven't found any material telling me how.
I also appreciate that if anyone can tell me how to make these two process read and write the same variable (same address in memory). This solves my problem, too.
There are lots of options, but in most cases I think you'll find that named pipes/fifo will meet your needs.
See mkfifo, which creates a named pipe on the filesystem; that pipe can then be opened and accessed using standard open/read/write like a file for interprocess communications.

Why would you use pointers for Low Memory Allocation? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
So I was looking at the source code of unreal engine and I came across this:
class USphereComponent* CollectionSphere;
There are using pointer for something that is going to be initialized and spawned once when the game begins and some objects will be overlapping it,I thought pointers are mainly used for more mid-high memory allocation purposes
So why are they using it now?
Keep in mind Im not really good at pointers so Im just trying learn and gather some information about it.
Whether to use pointers or not has almost nothing to do with "low" or "mid-high memory allocation purposes".
You use a pointer when you need a pointer, and often that's when the pointee (the thing being pointed to) will live for longer than an "automatic" object would in any of the program's existing, conveniently-accessible scopes.
Basically, *CollectionSphere will be a global (or something similar), but we can't construct it right away. The author has decided that they need fine control over the lifetime of the object. So, they have created a pointer to point to that object when the time comes.

What is the best way to call functions over a network? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I'm planning the network communications system for a program. The program is divided a client and a server class, but I might split them into separate programs if they get too big, and nothing else prevents it.
At the moment, all of the network communication is to call set and get functions on the server (of which their are about 500, and I expect to generate about 500-1,600 requests per second across eight clients in typical operations).
My current plan is to use a basic network API to send machine code inspired Opcodes to the server as a string, along with a few bytes of parameters, and have the server select the function and interpret the parameters using a massive switch statement.
However, with hundreds of possible function to call, this will get A) hard to read, and B)slow to implement. I'm also a bit concerned about the performance of large switch statements.
In light of that, could anyone confirm if this is the best way to implement function calls over a network, or tell me if there's a better way. I'd love a magic API that allows be to do something like x = callServerFunc(server, function, parameter1...) but my searches have not yet found such a library.
You can write your code from scratch. Feel free on how to do it.
Or you can use one of many ready mature technologies. Just google for CORBA, DCOM, XPCOM and so on. You can look at Remote call in common. Or consider existing web-based technologies with different data formats. This is just a starting point. This tehnologies are mature but heavyweight. You'll need some knowledge and practice to start with them.

Default exit function implementation [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I need to implement default behavior of exit call. I don't know what should I do and what is the most suitable way to do this. I have read that it should close file descriptors and something else.
Should I close default streams (stdout,err and in) ?
How to exit from nested functions calls ? Using goto is bad practice, what is the best way to break out ?
Thanks.
Do all of the things listed in exit(3), then invoke the _exit(2) system call. Alternatively, use longjmp(3) to jump back up to the main() function, then return from it. This invokes the same behavior as calling exit(3), and is just as dependent on the C runtime, so if exit(3) is unavailable for some reason, returning from main() will probably not work correctly either.
Unfortunately, AFAIK there is no portable way to enumerate all of the functions which may have been registered with atexit(3) and on_exit(3), so you'll have to keep track of those manually (i.e. every time you call atexit(3) or on_exit(3), append the function pointer to a list). Flushing stdio(3) is 3 straightforward fflush(3) calls.
You do not need to close any streams or file descriptors; the OS should do that automatically (the OS must not leak streams and fd's, so it is responsible for cleaning them up).
NB: longjmp() is almost always wrong under C++; throw an exception instead. It generally should only be used under straight C.

Can I pass *array to an EXE? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Is it possible to pass a pointer to an array from my app to a console exe program?
The scenario is: my app needs for certain cases some extra things to be done to the content of the array which is done by the EXE. So if its possible to send a pointer to the EXE and return it after modification ?
To exchange data between separate processes, you need a means of inter-process communication.
Options include:
shared memory
pipes
possibly even sockets
Which of the above is appropriate for you depends on the type and amount of data you want to exchange, how frequently you want to exchange it, etc.
You can do so by using the execv*() functions to start the new process from the process which provides the array, but are limited to an array of character pointers (char *).
No. The parameters given to a new process by the operating systems are an array of strings. You can neither replace that array nor make one of the strings an array.