Thread: Reading from and writing to memory with C

  1. #1
    Living to Learn
    Join Date
    Feb 2006
    Location
    Aberdeen, UK
    Posts
    33

    Question Reading from and writing to memory with C

    Hello. I've been looking at reading and writing to memory using C recently and would be grateful for your help. I've put together these two programs, but I know they're wrong.

    Code:
    char memstring[];
      for(i=0; i<=4096; i++)
    // go through the memory from 0x000 to 0xFFF
        int*ptr=0xi;
    // Pointed to memory location = I got this from Google
        memstring[] = i;
    // Assign the contents of each memory location to an entry in the array
    Code:
    char memstring; int location;
      for(i=0; i<=4096; i++)
    // Go through the memory locations from 0x000 to 0xFFF
        int*ptr=0xi;
    // Pointer to memory location - I got this off Google
      if(i = location)
    // When we reach the location identified in the location variable
        memstring = i;
    // Assign it's contents to the string variable
    I don't know if I've got the memory location bits right, as well as accessing the memory in the first place. Also, as it's an int we're dealing with, FFF wouldn't work, so I thought I'd have to write it as 4096, but then I'm not sure how to convert it back into hex.

    As said before, I really do appreciate your help. I tried searching on Google, this board and different groups, but there doesn't seem to be much info on this particular topic.

    Thanks again in advance.

    Hussein.

  2. #2
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    Code:
    int*ptr=0xi;
    Hex is just a way of presenting the number. Hex is base 16 and decimal is base 10. That means it doesn't matter if you assign an integer variable the number 0x15 or 21. It would be the same in both cases.

    As for the program, it will probably segfault, even if you're just reading the memory and not fiddling with it.

  3. #3
    Living to Learn
    Join Date
    Feb 2006
    Location
    Aberdeen, UK
    Posts
    33
    Sorry you lost me on the segfault. I'm just starting out and exporing different branches of C, working through some tutorials and books.

    Could you possibly explain a little clearer? Sorry for this - I'm just so keen to learn whatever I can!

    Hussein.

  4. #4
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    segfault = segmentation fault = Accessing memory that doesn't belong to you.
    http://en.wikipedia.org/wiki/Segmentation_fault

  5. #5
    Living to Learn
    Join Date
    Feb 2006
    Location
    Aberdeen, UK
    Posts
    33
    Hi there. Thanks for that - I feel so stupid for not looking it up before!

    Anyways, I understand in practice it wouldn't work, but would the problem work in theory? On a coding level, have I done it right? I don't have a way of testing it hence my asking. What I've tried to do is treat the memory like just a series of slots, say in an array, and basically accessing them, but I don't know if I've written it right as it's memory and not an array.

    Thanks.

  6. #6
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    Compile and see if the compiler likes it.

  7. #7
    Living to Learn
    Join Date
    Feb 2006
    Location
    Aberdeen, UK
    Posts
    33
    OK. Thanks.

  8. #8
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    u got make a note that high-level language dons't allow direct access of memory. to be precise the operating system dost allow you to do that.

    ssharish2005

Popular pages Recent additions subscribe to a feed