简体   繁体   中英

How to read the variable value from RAM?

I've written a program using dynamic memory allocation. I do not use the free function to free the memory, still at the address, the variable's value is present there.

Now I want to reuse this value and I want to see all the variables' values that are present in RAM from another process.

Is it possible?

#include<stdio.h>
#include<stdlib.h>
void main(){

    int *fptr;

    fptr=(int *)malloc(sizeof(int));

    *fptr=4;

    printf("%d\t%u",*fptr,fptr);
    while(1){
           //this is become infinite loop
     }

}

and i want to another program to read the value of a because it is still in memory because main function is infinite. how can do this?

This question shows misconceptions on at least two topics.

The first is virtual address spaces and memory protection, as already addressed by RobertL in his answer. On a modern operating system, you just can't access memory belonging to another process (even if you knew the physical address, which you don't because all user space processes work on addresses in their private virtual address space). The result of trying to access something not mapped into your address space will be a segmentation fault. Read more on Wikipedia .

The second is the scope of the C standard. It doesn't know about processes. C is defined in terms of an abstract machine executing your program (and only this program). Scopes and lifetimes of variables are defined and the respective maximum is the global scope and a static storage duration . So yes, your variable will continue to live as long as your program runs, but it's scope will be this program .

When you understand that, you see: even on a platform using a single global address space and no memory protection at all, you could never access the variable of another program in terms of the C standard. You could probably pass a pointer value somehow to your other program and use that, maybe it would work, but it would be undefined behavior .

That's why operating systems provide means for inter process communication like shared memory (which comes close to what you seem to want) and pipes .

When you return from main() , the process frees all acquired resources, so you can't. Have a look on this .

First of all, when you close your process the memory you allocated with it will be freed entirely. You can circumvent this by having 1 process of you write to a file (like a .dat file) and the other read from it. There are other ways , too.

But generally speaking in normal cases, when your process terminates the existing memory will be freed.

If you try accessing memory from another process from within your process, you will most likely get a segmentation fault, as most operating systems protect processes from messing with each other's memory.

It depends on the operating system. Most modern multi-tasking operating systems protect processes from each other. Hardware is setup to disallow processes from seeing other processes memory. On Linux and Unix for example, to communicate between programs in memory you will need to use operating system services for "inter-process" communication, such as shared memory, semaphores, or pipes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM