Reading memory-adresses from files

S

Sebastian Becker

Hello NG,

is fscanf capable of reading a memory adress from a file? I tried to
find the right format-parameter, but my code doesn't seem to work...

Regards,
Sebastian



----cut-----
int i=120;
int *a;
FILE *Outfile;
if ((Outfile = fopen("tst.dat","w"))== NULL) //open
{
if ((Outfile = fopen("tst.dat","w+"))==NULL) //create
{
printf("Error opening file\n");
exit();
}
}
printf("%p\n",&i);

fprintf(Outfile,"%p\n",&i);
fclose(Outfile);
Outfile = fopen("tst.dat","r");

fscanf(Outfile,"%p",a);
fclose(Outfile);
printf("%p",a);
----cut-------
 
V

Victor Bazarov

Sebastian said:
is fscanf capable of reading a memory adress from a file? I tried to
find the right format-parameter, but my code doesn't seem to work...

Any number can mean whatever you make it mean. For example, something
like 0x10000000 on Windows may mean the address from which a DLL is
loaded. It may actually be a valid address once you read that number
from a file and convert into an address.

When a program runs, objects in it are located at some addresses that
can be converted to numbers (long is usually large enough to represent
any object pointer value). While the program is running you can write
those values to a file and later (while the program is still running)
retrieve them from that file and use after converting back to pointers.
It is possible that those addresses will stay valid during the run of
the program, and you can actually know that yourself: pointers' lives
are well defined.

When the program exits, finishes, the memory it used to occupy and use
for the objects, is usually freed. Nothing can guarantee that the next
run of the same program will mean that the same objects are located at
the same memory addresses, generally.

So, the answer to your question is probably: while it is possible for
your program to store and retrieve addresses in/from files, there is no
much sense in doing that.

V
 
S

Sebastian Becker

It may actually be a valid address once you read that number
from a file and convert into an address.

This is exactly what I want to do, but my code doesn't work.

I want use a preemptiv multitasking "core" [1] to run 3-4 programs at
the same time. For IPC purposes there is a shared memory needed. Now, my
idea was to initialize variables in the first program (which will be
running the whole time) and hand over the adresses to the other
programs. Doing this the CPU can remain in real-mode.
So, the answer to your question is probably: while it is possible for
your program to store and retrieve addresses in/from files, there is no
much sense in doing that.

So, how do you program this retrival of pointers from files?

Thanks, Sebastian


[1] http://www.shamrock.de/dostools.htm#multitasking - unfortunately
only in german

mt.com [Parameters: filenames of the tasks (exe)]
- runs in real-mode
- preemptive multi-tasking
- tasks are switched by a reprogrammed keyboard interrupt
 
V

Victor Bazarov

Sebastian Becker said:
It may actually be a valid address once you read that number
from a file and convert into an address.

This is exactly what I want to do, but my code doesn't work.

I want use a preemptiv multitasking "core" [1] to run 3-4 programs at the
same time. For IPC purposes there is a shared memory needed. Now, my idea
was to initialize variables in the first program (which will be running
the whole time) and hand over the adresses to the other programs. Doing
this the CPU can remain in real-mode.
So, the answer to your question is probably: while it is possible for
your program to store and retrieve addresses in/from files, there is no
much sense in doing that.

So, how do you program this retrival of pointers from files?

Just like I said, convert them to integers when printing out and then
read them as integers and convert from integers to pointers after they
have been read.

V
 
S

Sebastian Becker

Just like I said, convert them to integers when printing out and then
read them as integers and convert from integers to pointers after they
have been read.

Thanks, works fine now.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top