Thread: Inputting directly into memory

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    32

    Inputting directly into memory

    Hi again,

    Is there a way to input directly into the memory and then access the input through a pointer?

  2. #2
    Registered User Pioneer's Avatar
    Join Date
    Dec 2002
    Posts
    59
    It depends on what you mean by directly into memory, this works:
    Code:
    main(){
        int x;
        int *p = &x;
    
        *p = 10;
    }
    But if you mean directly to a specific memory address then I would say if you don't know how to do it, you aren't supposed to:
    Code:
    main(){
        unsigned int *p = (unsigned int *)0x12345678;
    }

  3. #3
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    i agree, rarely will you ever need to point at absolute memory addresses, and when you need to, you'll know how to do it. if you would like the program to set aside some memory for you and then write to that, use malloc ().
    hello, internet!

  4. #4
    Visionary Philosopher Sayeh's Avatar
    Join Date
    Aug 2002
    Posts
    212
    Is there a way to input directly into the memory and then access the input through a pointer?
    So, just what exactly do you _think_ is happening when you access a variable?!?!

    Code:
    char  name[5];
    
    int main(void)
       {
       name[0] = 'W';              /* access address via pointer */
       name[1] = 'O';
       name[2] = 'W';
       name[3] = 0x00;
       
       return(0);
       };
    The array 'name[]' is at a specific address in RAM. Stuffing a letter into each position of the array means the address of the array and it's offset must be obtained and then data copied into that location.
    It is not the spoon that bends, it is you who bends around the spoon.

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    >> The array 'name[]' is at a specific address in RAM

    It seems to me people used the word "fixed." name[] isn't always in the same place whereas 0x12345678 is. At the machine level accessing memory locations is okay. In other words, unless you are making a video card driver or something to that effect you won't be writing anything like int *i = (int *)0xA1FFFF000

  6. #6
    Visionary Philosopher Sayeh's Avatar
    Join Date
    Aug 2002
    Posts
    212
    It seems to me people used the word "fixed." name[] isn't always in the same place whereas 0x12345678 is.
    The original requestor didn't ask for the same address in RAM, they asked, instead, how to put a value directly into RAM.

    That was the question I answered.

    If you want to stuff a value directly into a never-changing address (ignoring seg faults and other gotchas), then:

    Code:
    unsigned char *theAddressP;
    
    theAddressP = 0x004E37A9;                        /* arbitrary address */
    theAddressP[0] = 'a';                                    /* stuff an a there */
    And even that doesn't put it in the same physical location-- the value 0x004E37a9 (or any other) is relative to the start address of your heap zone. Unless you're working in the O/S Heap.

    *************
    WARNING
    *************
    The above code corrupts memory. It is for educational purposes only.
    Last edited by Sayeh; 12-10-2002 at 08:25 AM.
    It is not the spoon that bends, it is you who bends around the spoon.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-06-2009, 12:27 PM
  2. Copying memory, pointers and the like.
    By psychopath in forum C++ Programming
    Replies: 34
    Last Post: 12-12-2006, 01:37 PM
  3. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  4. Memory leak trackers
    By Darkness in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 11-30-2004, 02:03 PM
  5. Accessing memory directly
    By YALINI in forum C Programming
    Replies: 0
    Last Post: 08-30-2001, 11:56 PM