access memory location

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ivan

    #1

    access memory location

    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


  • Daniel Fischer

    #2
    Re: access memory location

    On Wed, 16 Nov 2005 18:04:26 +0000, Ivan wrote:
    [color=blue]
    > how can I access the contents of a memoy location say at address 0x3FF
    > in C?[/color]

    Not at all, but
    [color=blue]
    > unsigned int *wVal = 0x3FF;
    > x = *wVal;[/color]

    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

    Comment

    • Jonathan Mcdougall

      #3
      Re: access memory location

      Ivan wrote:[color=blue]
      > 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;[/color]

      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_cas t<unsigned int*>(0x3FF);


      Jonathan

      Comment

      • red floyd

        #4
        Re: access memory location

        Jonathan Mcdougall wrote:[color=blue]
        > Ivan wrote:
        >[color=green]
        >>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;[/color]
        >
        >
        > 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_cas t<unsigned int*>(0x3FF);
        >[/color]

        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.

        Comment

        • Emmanuel Delahaye

          #5
          Re: access memory location

          Ivan a écrit :[color=blue]
          > how can I access the contents of a memoy location say at address 0x3FF in C?[/color]

          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

          Comment

          • Greg

            #6
            Re: access memory location

            Ivan wrote:[color=blue]
            > 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;[/color]

            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

            Comment

            • Ivan

              #7
              Re: access memory location


              "red floyd" <no.spam@here.d ude> wrote in message
              news:13Lef.1695 2$q%.6643@newss vr12.news.prodi gy.com...[color=blue]
              > Jonathan Mcdougall wrote:[color=green]
              > > Ivan wrote:
              > >[color=darkred]
              > >>Hi
              > >>
              > >>how can I access the contents of a memoy location say at address 0x3FF[/color][/color][/color]
              in C?[color=blue][color=green][color=darkred]
              > >>
              > >>I tried this but didn't work
              > >>
              > >>unsigned int *wVal = 0x3FF;
              > >>x = *wVal;[/color]
              > >
              > >
              > > 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_cas t<unsigned int*>(0x3FF);
              > >[/color]
              >
              > 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.
              >[/color]
              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


              Comment

              • red floyd

                #8
                Re: access memory location

                Ivan wrote:[color=blue]
                > "red floyd" <no.spam@here.d ude> wrote in message
                > news:13Lef.1695 2$q%.6643@newss vr12.news.prodi gy.com...
                >
                > 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
                >[/color]

                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_cas t<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).


                Comment

                Working...