0

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?

Tiger
  • 136
  • 9
  • Are you asking if the memory at fptr will be 4 after the program exits? – RobertL Oct 27 '15 at 07:20
  • no we just write a code "int a=5; while(1){ }" then it will infinite loop and now i want run another program and to read that value because here not end the main function so value is still in ram not another program to read the value of variable a – Tiger Oct 27 '15 at 07:25
  • Then you should include that in your question. I already edited it in (reedit if your not content) but memorize that for the future. – cadaniluk Oct 27 '15 at 07:26
  • This might help: http://stackoverflow.com/questions/5656530/how-to-use-shared-memory-with-linux-in-c – cadaniluk Oct 27 '15 at 07:29
  • [don't cast the result of malloc in C](http://stackoverflow.com/q/605845/995714) – phuclv Oct 27 '15 at 07:39

4 Answers4

3

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.

  • i am not able to give to up vote :) if i m able will give you up vote :) thnkq for grate explain – Tiger Oct 27 '15 at 08:22
0

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

Community
  • 1
  • 1
Mohan
  • 1,871
  • 21
  • 34
  • ohkkk then suppose we just write a code
    int a=5;
    while(1){
    
    }
    
    then it will infinite loop and now i want run another program and to read that value because here not end the main function so value is still in ram
    – Tiger Oct 27 '15 at 07:20
  • It depends on the operating system. Most modern multi-tasking operating systems protect processes from each other. – RobertL Oct 27 '15 at 07:28
0

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.

Community
  • 1
  • 1
Magisch
  • 7,312
  • 9
  • 36
  • 52
0

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.

RobertL
  • 365
  • 1
  • 9
  • but why we can not do this, in c language pointer get the value at address from the ram m i right? – Tiger Oct 27 '15 at 07:36
  • That would only work on MS-DOS and Apple ][ and other computers without modern multi-tasking operating systems. The operating sets up the hardware so that if your program tries to access another programs memory, you will get an error. – RobertL Oct 27 '15 at 07:45