access memory location

Discussion in 'C++' started by Ivan, Nov 16, 2005.

  1. Ivan

    Ivan Guest

    Hi

    how can I access the contents of a memoy location say at address 0x3FF in C?

    I tried this but didn't work

    unsigned int *wVal = 0x3FF;
    x = *wVal;


    Thanks
    ivan
     
    Ivan, Nov 16, 2005
    #1
    1. Advertising

  2. On Wed, 16 Nov 2005 18:04:26 +0000, Ivan wrote:

    > how can I access the contents of a memoy location say at address 0x3FF
    > in C?


    Not at all, but

    > unsigned int *wVal = 0x3FF;
    > x = *wVal;


    might work on some platforms. It's not valid C, of course.

    The address being odd might be a problem on some machines. Try accessing
    it through an unsigned char * instead of int *, which needs to be aligned
    on many platforms.


    Daniel
     
    Daniel Fischer, Nov 16, 2005
    #2
    1. Advertising

  3. Ivan wrote:
    > Hi
    >
    > how can I access the contents of a memoy location say at address 0x3FF in C?
    >
    > I tried this but didn't work
    >
    > unsigned int *wVal = 0x3FF;
    > x = *wVal;


    That's undefined behavior in standard C++, but it may work on your
    platform. You have to cast it explicitly because 0x3FF is an int and
    ints cannot be implicitly converted to pointers.

    unsigned int *wVal = reinterpret_cast<unsigned int*>(0x3FF);


    Jonathan
     
    Jonathan Mcdougall, Nov 16, 2005
    #3
  4. Ivan

    red floyd Guest

    Jonathan Mcdougall wrote:
    > Ivan wrote:
    >
    >>Hi
    >>
    >>how can I access the contents of a memoy location say at address 0x3FF in C?
    >>
    >>I tried this but didn't work
    >>
    >>unsigned int *wVal = 0x3FF;
    >>x = *wVal;

    >
    >
    > That's undefined behavior in standard C++, but it may work on your
    > platform. You have to cast it explicitly because 0x3FF is an int and
    > ints cannot be implicitly converted to pointers.
    >
    > unsigned int *wVal = reinterpret_cast<unsigned int*>(0x3FF);
    >


    What, exactly, is the OP trying to accomplish? Is he trying to tweak an
    interrupt vector or memory mapped I/O?

    Unless you're on a system where there's no memory management, there's no
    guarantee that 0x3ff will be a valid memory address in your address
    space. If you're attempting to access a memory-mapped I/O port, or some
    system structure, you're toast, under a virtual memory system.

    If you're writing an OS kernel or run-time executive, or you're in an
    environment where there's no MMU, then that's an entirely different
    kettle of fish.
     
    red floyd, Nov 16, 2005
    #4
  5. Ivan a écrit :
    > how can I access the contents of a memoy location say at address 0x3FF in C?


    Memory location is a vague concept. If you meant physical address, you
    can try

    unsigned int *wVal = (unsigned int *)0x3FF;

    but the result depends on the implementation. Even if this is
    compilable, the value can be a physical address or not. If your system
    has a MMU (Memory Management Unit), the physical addresses can only be
    accessed under special conditions (Kernel mode, ring0 etc.), like the
    ones in a driver.

    --
    A+

    Emmanuel Delahaye
     
    Emmanuel Delahaye, Nov 16, 2005
    #5
  6. Ivan

    Greg Guest

    Ivan wrote:
    > Hi
    >
    > how can I access the contents of a memoy location say at address 0x3FF in C?
    >
    > I tried this but didn't work
    >
    > unsigned int *wVal = 0x3FF;
    > x = *wVal;


    Consult your compiler's documentation to see if it supports declaring
    variables at specific memory addresses. Metrowerks Codewarrior for
    example has a specific syntax to do so. And I would imagine that almost
    any compiler that supports embedded systems would have a similar
    capability.

    Greg
     
    Greg, Nov 16, 2005
    #6
  7. Ivan

    Ivan Guest

    "red floyd" <> wrote in message
    news:13Lef.16952$q%...
    > Jonathan Mcdougall wrote:
    > > Ivan wrote:
    > >
    > >>Hi
    > >>
    > >>how can I access the contents of a memoy location say at address 0x3FF

    in C?
    > >>
    > >>I tried this but didn't work
    > >>
    > >>unsigned int *wVal = 0x3FF;
    > >>x = *wVal;

    > >
    > >
    > > That's undefined behavior in standard C++, but it may work on your
    > > platform. You have to cast it explicitly because 0x3FF is an int and
    > > ints cannot be implicitly converted to pointers.
    > >
    > > unsigned int *wVal = reinterpret_cast<unsigned int*>(0x3FF);
    > >

    >
    > What, exactly, is the OP trying to accomplish? Is he trying to tweak an
    > interrupt vector or memory mapped I/O?
    >
    > Unless you're on a system where there's no memory management, there's no
    > guarantee that 0x3ff will be a valid memory address in your address
    > space. If you're attempting to access a memory-mapped I/O port, or some
    > system structure, you're toast, under a virtual memory system.
    >
    > If you're writing an OS kernel or run-time executive, or you're in an
    > environment where there's no MMU, then that's an entirely different
    > kettle of fish.
    >

    it is an embedded system, I thought there was a standard way of doing it,
    well I suppose I'll have to contact them to know how to do it.

    thanks you all
     
    Ivan, Nov 16, 2005
    #7
  8. Ivan

    red floyd Guest

    Ivan wrote:
    > "red floyd" <> wrote in message
    > news:13Lef.16952$q%...
    >
    > it is an embedded system, I thought there was a standard way of doing it,
    > well I suppose I'll have to contact them to know how to do it.
    >
    > thanks you all
    >


    Then Johnathan is correct. The main issue I see is the potential
    misalignment.

    Assuming you do in fact have direct hardware access, and that you really
    want an unsigned at that alignment, you should use one of the two
    constructs:

    // C style (you did crosspost to c.l.c)
    unsigned int *const wPtr = ((unsigned int *)0x3ff);

    or

    // C++ style (you did crosspost to c.l.c++)
    unsigned int *const wPtr = reinterpret_cast<unsigned int *>(0x3ff);

    if 0x3ff is a read-only memory address, make it

    unsigned int const *const wPtr = ...;

    If 0x3ff is a memory mapped register, which can change under you, make
    it volatile as well (volatile goes before the * in the declaration, both
    sides of the assignment).
     
    red floyd, Nov 16, 2005
    #8
    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. Luke Dalessandro
    Replies:
    0
    Views:
    614
    Luke Dalessandro
    Jan 15, 2006
  2. TIM
    Replies:
    9
    Views:
    557
    Kevin Goodsell
    Apr 12, 2004
  3. zxo102
    Replies:
    0
    Views:
    417
    zxo102
    Aug 1, 2006
  4. Ivan

    access memory location

    Ivan, Nov 16, 2005, in forum: C Programming
    Replies:
    7
    Views:
    353
    red floyd
    Nov 16, 2005
  5. =?Utf-8?B?SUthdHphdg==?=
    Replies:
    0
    Views:
    1,278
    =?Utf-8?B?SUthdHphdg==?=
    Jun 11, 2007
Loading...

Share This Page