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.
Related
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 2 years ago.
Improve this question
As far as I know, a memory address like 0x0 of a program is the beginning of its code segment in its virtual address space. Is it possible to simply read what's in there from within a program? What about checking things like the size of the stack/heap? If not possible in C/++ programs, is it possible in assembly?
Edit: I find memory allocation and management interesting. I'm asking out of curiosity. I like the idea of being able to see what's in every address of my program's virtual address space. When I mentioned stack/heap size, I meant those of the program, too.
First, check /proc/<pid>/maps, assuming that you are running Linux. This will show you a list of allocated regions, and the permissions for each region (or VMA, technically). Check out this answer. The permissions are 'rwx' or a subset of these, representing readable, writable and executable. For regions which are readable, you can craft a pointer in C/C++ using a uintptr_t. Thereafter, you can read it.
Basically, you can dump all of your readable regions using simple pointers.
Btw, in virtually all C binaries, the region starting at address 0x0 will be unmapped, so that using a NULL pointer leads to a SEGFAULT.
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.
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 8 years ago.
Improve this question
I need a temporary string to append and modify pre-existing strings so that i can use with DrawText. That temporary string needs to change inside a function so I have 2 options:
-Use std::string::clear()
-Initialize another temporary string.
I can use and understand both methods but I'm wondering, which one is better?
Edit: To the function in question, having low running-time is essential
Whichever one more clearly reflects the intent of the code is better. The one you would use if you didn't stop to think which was "better" is better.
If (and only if) profiling reveals a performance problem in your function then you might save tiny amounts of time by reusing an existing string.
The memory of the existing string is already allocated. No new memory allocation needs to be made unless the string exceeds the size of the memory allocation.
On the other hand, if you create and destroy a lot of strings, the allocation time can start to add up.
I have some code where std::string allocation and copying dominates the profile. To fix it sometime in the future, we're going to have to implement string pooling, custom allocators and use string_ref instead of string.
So yes, it can be a problem. But measure to find out before trying to fix it.
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 am writing a program which reads from a file and processes the data in the file. Each line in the file is an entity. There are 85620 lines in the file. Is it efficient to define a class of the entity and have 85620 instances of that class?
Depends a bit on the class. But in general, 80k objects is a non-concern.
Yes, especially if you're storing them in a memory-efficient container like std::vector (hint: reserve some space up front if you know you always need at least a thousand or so).
It depends on a lot of factor, and the target software/hardware requirements.
On modern computer with 8GB+ memory, 80k object with even 1K each would not be a big deal, however it may be an issue on mobile phone or embedded systems.
Also note that if you are doing single pass processing and do not need the data afterward, there is no reason to store them.
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
Generally when do you allocate memory in C++, you have new/delete and virtualalloc, amongst a few other API calls, is the generally for dynamic allocation, but then we have vector and such so what are the common uses for allocating memory?
If you don't know, at compilation time, how many items you will need, your best option is to use dynamic allocation.
That way you can (hopefully) deal with all the input without wasting memory by reserving an humongous space with a big array.
// ...
int humongous[10000]; // I only expect 10 items, so this should be enough for creative users
// ...
If you want to deal with large memory (i.e memory that can't be allocated on stack) then you can use dynamic allocation.
As a general answer: "there may be cases where the memory needs of a program can only be determined during runtime. For example, when the memory needed depends on user input. On these cases, programs need to dynamically allocate memory, for which the C++ language integrates the operators new and delete."
source: http://www.cplusplus.com/doc/tutorial/dynamic/