i want to access to a specific memory unit

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

    i want to access to a specific memory unit

    Well, i have a question here. i want to write 50000 into a specific
    memory unit (like # 4321). So i wrote these:

    int number=5000;
    memcpy((char *)50000,(char *)&number,sizeo f(char));
    char * p=(char *)50000;
    cout<<"This is p"<<endl;
    cout<<p<<endl;

    But it doesn't work, how can i access to a specific memory unit.
    Thank you.
  • Ian Collins

    #2
    Re: i want to access to a specific memory unit

    Vincent SHAO wrote:
    Well, i have a question here. i want to write 50000 into a specific
    memory unit (like # 4321). So i wrote these:
    >
    int number=5000;
    memcpy((char *)50000,(char *)&number,sizeo f(char));
    char * p=(char *)50000;
    cout<<"This is p"<<endl;
    cout<<p<<endl;
    >
    But it doesn't work, how can i access to a specific memory unit.
    The answer lies in the implementation specific realm, you had better ask
    on a group dedicated to your tools and or platform.

    --
    Ian Collins.

    Comment

    • Juha Nieminen

      #3
      Re: i want to access to a specific memory unit

      Vincent SHAO wrote:
      Well, i have a question here. i want to write 50000 into a specific
      memory unit (like # 4321). So i wrote these:
      >
      int number=5000;
      memcpy((char *)50000,(char *)&number,sizeo f(char));
      char * p=(char *)50000;
      cout<<"This is p"<<endl;
      cout<<p<<endl;
      >
      But it doesn't work, how can i access to a specific memory unit.
      Thank you.
      "It doesn't work" tells nothing to us. What are the *symptoms* you are
      seeing? Is it crashing? Is it returning the wrong value? What?

      Besides, the above code is horribly non-portable, and thus not really
      on-topic in this group.

      Comment

      • keith@bytebrothers.co.uk

        #4
        Re: i want to access to a specific memory unit

        On 4 Mar, 08:41, Vincent SHAO <vincent.shao.b ...@gmail.comwr ote:
        Well, i have a question here. i want to write 50000 into a specific
        memory unit (like # 4321). So i wrote these:
        >
        memcpy((char *)50000,(char *)&number,sizeo f(char));
        First, you should ask someone about the difference between 'source'
        and 'destination'.

        Then, ask them to explain why it is unlikely that the value 50000
        would fit into something that was 'sizeof(char)' bytes long.

        Then after all that, perhaps you yourself could explain why you think
        that '(char*)&number ' might have anything to do with 'a specific
        memory unit (like # 4321)'.

        HTH.

        Comment

        • James Kanze

          #5
          Re: i want to access to a specific memory unit

          On Mar 4, 9:57 am, Ian Collins <ian-n...@hotmail.co mwrote:
          Vincent SHAO wrote:
          Well, i have a question here. i want to write 50000 into a specific
          memory unit (like # 4321). So i wrote these:
          int number=5000;
          memcpy((char *)50000,(char *)&number,sizeo f(char));
          char * p=(char *)50000;
          cout<<"This is p"<<endl;
          cout<<p<<endl;
          But it doesn't work, how can i access to a specific memory unit.
          The answer lies in the implementation specific realm, you had
          better ask on a group dedicated to your tools and or platform.
          Mostly. The C++ part of the answer is reinterpret_cas t.

          As it is, his comments don't seem to correspond to his code, and
          his code seems to be trying to write 5000 into a char, which
          isn't going to work on a lot of systems, for obvious reasons.
          If I understand what he's trying to do, however, something like:
          *reinterpret_ca st< int* >( 50000 ) = 5000 ;
          should do the trick, although what that will actually do is, of
          course, very, very implementation dependent. (It will generate
          a core dump on the implementations I use, unless the executable
          is very, very small.)

          --
          James Kanze (GABI Software) email:james.kan ze@gmail.com
          Conseils en informatique orientée objet/
          Beratung in objektorientier ter Datenverarbeitu ng
          9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

          Comment

          • Vincent SHAO

            #6
            Re: i want to access to a specific memory unit

            On Mar 4, 5:07 pm, Juha Nieminen <nos...@thanks. invalidwrote:
            Vincent SHAO wrote:
            Well, i have a question here. i want to write 50000 into a specific
            memory unit (like # 4321). So i wrote these:
            >
            int number=5000;
            memcpy((char *)50000,(char *)&number,sizeo f(char));
            char * p=(char *)50000;
            cout<<"This is p"<<endl;
            cout<<p<<endl;
            >
            But it doesn't work, how can i access to a specific memory unit.
            Thank you.
            >
            "It doesn't work" tells nothing to us. What are the *symptoms* you are
            seeing? Is it crashing? Is it returning the wrong value? What?
            >
            Besides, the above code is horribly non-portable, and thus not really
            on-topic in this group.
            Thanks for comments. The original problem is "put a byte(like 34) into
            a specific memory unit (like #4321)". Here is my code
            //define a byte
            char number=34;
            //copy the number to the memory unit #4321
            memcpy((char *)4321,(char *)&number,sizeo f(char));
            char * p=(char *)4321;
            cout<<(int)*p<< endl;

            i run the program and there is nothing out put.

            And why the former code is non-portable, how judge?
            Thank you

            Comment

            • Jeff Schwab

              #7
              Re: i want to access to a specific memory unit

              Vincent SHAO wrote:
              Well, i have a question here. i want to write 50000 into a specific
              memory unit (like # 4321). So i wrote these:
              >
              int number=5000;
              memcpy((char *)50000,(char *)&number,sizeo f(char));
              char * p=(char *)50000;
              cout<<"This is p"<<endl;
              cout<<p<<endl;
              You're outputting the pointer value, not the memory content.
              Dereference the pointer (cout << *p).
              But it doesn't work, how can i access to a specific memory unit.

              Comment

              • Victor Bazarov

                #8
                Re: i want to access to a specific memory unit

                Jeff Schwab wrote:
                Vincent SHAO wrote:
                > Well, i have a question here. i want to write 50000 into a specific
                >memory unit (like # 4321). So i wrote these:
                >>
                >int number=5000;
                >memcpy((char *)50000,(char *)&number,sizeo f(char));
                >char * p=(char *)50000;
                >cout<<"This is p"<<endl;
                >cout<<p<<end l;
                >
                You're outputting the pointer value, not the memory content.
                Dereference the pointer (cout << *p).
                I also failed to see '4321' in the code in any way. The usual
                (implementation-defined) method is to use the integral value and
                reinterpret_cas t it to the pointer. Perhaps this is what the
                intention of declaring 'p' was...

                char *p = reinterpret_cas t<char*>(4321) ;

                V
                --
                Please remove capital 'A's when replying by e-mail
                I do not respond to top-posted replies, please don't ask


                Comment

                • Ian Collins

                  #9
                  Re: i want to access to a specific memory unit

                  Vincent SHAO wrote:
                  On Mar 4, 5:07 pm, Juha Nieminen <nos...@thanks. invalidwrote:
                  >Vincent SHAO wrote:
                  >> Well, i have a question here. i want to write 50000 into a specific
                  >>memory unit (like # 4321). So i wrote these:
                  >> int number=5000;
                  >> memcpy((char *)50000,(char *)&number,sizeo f(char));
                  >> char * p=(char *)50000;
                  >> cout<<"This is p"<<endl;
                  >> cout<<p<<endl;
                  >>But it doesn't work, how can i access to a specific memory unit.
                  >>Thank you.
                  > "It doesn't work" tells nothing to us. What are the *symptoms* you are
                  >seeing? Is it crashing? Is it returning the wrong value? What?
                  >>
                  > Besides, the above code is horribly non-portable, and thus not really
                  >on-topic in this group.
                  >
                  Thanks for comments. The original problem is "put a byte(like 34) into
                  a specific memory unit (like #4321)". Here is my code
                  //define a byte
                  char number=34;
                  //copy the number to the memory unit #4321
                  memcpy((char *)4321,(char *)&number,sizeo f(char));
                  char * p=(char *)4321;
                  cout<<(int)*p<< endl;
                  >
                  Why make things so complex? sizeof(char) is 1 by definition, so you
                  could simply write

                  char* p = reinterpret_cas t<char*>(4321) ;
                  *p = number;
                  i run the program and there is nothing out put.
                  >
                  And why the former code is non-portable, how judge?
                  What are you running on and how do you know 4321 is a valid, writable
                  address?

                  --
                  Ian Collins.

                  Comment

                  Working...