Reading memory-adresses from files

Discussion in 'C++' started by Sebastian Becker, Jan 11, 2005.

  1. 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-------
    Sebastian Becker, Jan 11, 2005
    #1
    1. Advertising

  2. Sebastian Becker wrote:
    > 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
    Victor Bazarov, Jan 11, 2005
    #2
    1. Advertising

  3. > 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
    Sebastian Becker, Jan 12, 2005
    #3
  4. "Sebastian Becker" <> wrote...
    >> 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
    Victor Bazarov, Jan 12, 2005
    #4
  5. > 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.
    Sebastian Becker, Jan 12, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jason Robertson
    Replies:
    0
    Views:
    314
    Jason Robertson
    Nov 26, 2004
  2. Sascha Teifke
    Replies:
    2
    Views:
    2,773
    Sascha Teifke
    Jun 28, 2003
  3. Replies:
    3
    Views:
    421
    Roedy Green
    Feb 24, 2006
  4. Kevin Joplin

    member function adresses

    Kevin Joplin, Oct 27, 2003, in forum: C++
    Replies:
    3
    Views:
    312
    Kevin Joplin
    Oct 27, 2003
  5. Nikhil
    Replies:
    4
    Views:
    957
    Martin Gregorie
    Apr 15, 2007
Loading...

Share This Page