#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    74
    Rep Power
    11

    Absolute Addressing


    I am trying to create registers which are at specific memory locations. I am halfway there, but can't seem to figure out the rest. Here is what I have so far:


    typedef struct {
    int x;
    int y;
    } MyStruct;

    #define BASE (ox80000000)

    MyStruct* abc = (MyStruct*)BASE;

    // Now abc is a pointer to a "MyStruct" at the address
    // 0x80000000.

    This I get compilied and I can print out the address and everything is fine. How do I go about using this now?

    I want to be able to have a struct where I can change/update the values and have a pointer which always points to the same address where that struct is being updated. Is this easier in C++ where I can use the NEW operator? How would that be done?


    Any leads/help is appreciated.
  2. #2
  3. Left due to despotic ad-min
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Posts
    1,044
    Rep Power
    14
    You're mixing up concepts.

    Your code is actually invoking undefined, by treating the memory at 0x80000000 (not ox80000000 which I assume was a typo) as if it points to an object of type MyStruct.

    The ciurcumstances in which you need to do this are extremely rare. For example, when writing device drivers. If you had the skills to write a device driver, you would not be wondering what you are doing: you would know.

    From your description I suspect you're actually trying to work out how to share a structure between two source files. The way to do it is something like;

    // in a header file

    typedef struct {
    int x;
    int y;
    } MyStruct;

    extern MyStruct *object;

    void f(); /* I'll use this for illustrative purposes */

    // in source file 1

    #include "header.h"
    MyStruct the_object;

    MyStruct *object = &the_object;

    void f()
    {
    object->x *= 2;
    object->y *= 2;
    }

    // in source file 2

    #include <stdio.h>
    #include "header.h"

    int main()
    {
    object->x = 1;
    object->y = 1;
    f();
    fprintf(stdout, "%d %d\n", object->x, object->y);
    return 0;
    }
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    74
    Rep Power
    11
    Can't always assume. I am doing something similar to writing device drivers. I just haven't:

    1) Done it before.
    and
    2) Haven't used C in a while.
  6. #4
  7. Left due to despotic ad-min
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Posts
    1,044
    Rep Power
    14
    Some of the information you gave through me off. For example, you talked about using operator new, which is dynamic memory allocation --- it has nothing to do with accessing a fixed address. It is also C++, not C.

    In any event, if I'm now interpreting your question correctly... try this

    #include <stdio.h>

    typedef struct {
    int x;
    int y;
    } MyStruct;

    #define BASE (0x80000000) // your typo fixed!

    MyStruct* abc = (MyStruct*)BASE;

    int main()
    {
    // treat the memory at 0x80000000 as if it were a MyStruct

    // assign elements
    abc->x = 42;
    abc->y = 11;

    // print out abc->x twice

    fprintf(stdout, "%d %d\n", abc->x, (*abc).x);

    return 0;

    }

    If abc is an extern variable, declared in a header file as in my previous example, it can be accessed from multiple source files.
  8. #5
  9. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,109
    Rep Power
    1802
    What exactly are you trying to do, and what on?

    I ask because of a platform with a protected mode OS, (Windows, or Linux for example) you will not be able to do that without causing an exception.

    To access pgysical memory, your codes process must 'own' it. In Windows, memory is 'virtual', so an address translation will not normally map to the same physical address, and will more often than not generate a acception if it is not in the memory map of the process.

    In Windows, you will need to write a real device driver, using the Microsoft DDK.

    Clifford

IMN logo majestic logo threadwatch logo seochat tools logo