Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: how to write to a particular address in C?

  1. #1
    Join Date
    Aug 2012
    Beans
    601

    how to write to a particular address in C?

    Hello,
    I am trying to write to a specific memory location in C, but I keep getting a segmentation fault.
    All the addresses returned are in 7digit hexadecimal, and the address used in the below program (85a9008) was got printing the address from a previous program, which means that writing to this memory location is permitted.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
      char* address = (int *)0x85a9008;
      *address = 'a';
      printf("\n*address == [%c]",*address);
      printf("Memory address is: 0x%p\n", address);
    
      return 0;
    }
    Please advise.
    Thanks.

  2. #2
    Join Date
    Aug 2012
    Beans
    185

    Re: how to write to a particular address in C?

    You can only write to memory allocated and owned by your process.
    There are ways to explicitly share memory with other processes, but that's a different story, subject to explicit APIs and limitations.

    EDIT: in the old days, back like 20 years ago, you could actually directly access memory of any process, which was a major pain for anyone, except malware programmers.
    Last edited by bird1500; January 12th, 2013 at 04:59 PM.

  3. #3
    Join Date
    Aug 2012
    Beans
    601

    Re: how to write to a particular address in C?

    Quote Originally Posted by bird1500 View Post
    You can only write to memory allocated and owned by your process.
    There are ways to explicitly share memory with other processes, but that's a different story, subject to explicit APIs and limitations.

    EDIT: in the old days, back like 20 years ago, you could actually directly access memory of any process, which was a major pain for anyone, except malware programmers.
    Thanks bird1500 for the reply.

    1.So how do I find out the list of addresses that can be used by my process ?

    2.
    your process
    Does this mean, my program's executable say a.out that I am executing, or is the list of addresses pre-decided for all C programs or something ?

  4. #4
    Join Date
    Aug 2012
    Beans
    185

    Re: how to write to a particular address in C?

    The memory thingy is generally complicated, but in layman's terms:

    A program (that is a process) gets automatically some memory from the OS which puts the program into memory and executes it - let's call it static memory that your program can't write to, and probably can't read - not sure about this one.

    Then your program once launched can ask the OS (kernel) for memory it can read from and writeto, typically with "malloc()" for C or "new" for C++.

    Generally a program doesn't care how much memory it can get from the OS, it only checks if the request for new memory succeeded (and frees it if needed when done), if not - probably the OS is out of memory.

    For example, for C:
    char *buf = (char*) malloc(10);//allocated 10 bytes somewhere in computer's memory which is called "the heap".
    now, buf points to the first char,
    buf+5 points to the 6th char,
    buf+9 to the last one,
    if you try to access buf+10 or more you get an error (segmentation fault or so) because it's outside the range of the chunk of memory you asked for.
    Last edited by bird1500; January 12th, 2013 at 06:21 PM.

  5. #5
    Join Date
    May 2010
    Location
    SE England
    Beans
    210

    Re: how to write to a particular address in C?

    Quote Originally Posted by bird1500 View Post
    You can only write to memory allocated and owned by your process.
    There are ways to explicitly share memory with other processes, but that's a different story, subject to explicit APIs and limitations.

    EDIT: in the old days, back like 20 years ago, you could actually directly access memory of any process, which was a major pain for anyone, except malware programmers.

    A pain to most maybe but we used to regularly write direct to video ram and bypass the OS on DOS based systems - It was orders of magnitude faster than OS calls for screen handling!

    Bouncingwilf

  6. #6
    Join Date
    Aug 2006
    Location
    60°27'48"N 24°48'18"E
    Beans
    3,453

    Re: how to write to a particular address in C?

    Quote Originally Posted by IAMTubby View Post
    1.So how do I find out the list of addresses that can be used by my process ?
    To elaborate on this a little; processes (that is, whatever instance of a program you have launched) also operate within so-called virtual memory. The operating system abstracts away actual hardware memory addresses; your process sees essentially a contigous number of addresses that the CPU's memory-mapper then maps to hw addresses. So in that sense the concept of "list of addresses you may use" is a bit flawed.

    However, even addressing outside of explicitly reserved memory within the process-specific virtual memory space is a segfault. This is because addresses are just numbers and if the OS + CPU combination can't map some given number to an actual memory location, that's an error.
    LambdaGrok. | #ubuntu-programming on FreeNode

  7. #7
    Join Date
    Nov 2005
    Location
    Bordeaux, France
    Beans
    11,297
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: how to write to a particular address in C?

    Quote Originally Posted by IAMTubby View Post
    1.So how do I find out the list of addresses that can be used by my process ?
    You can't, as far as I know. Even if you could, that would be of limited usefulness. If you want to obtain a memory address you can write to, use malloc(), or the & operator on an object on the stack.

    2. Does this mean, my program's executable say a.out that I am executing, or is the list of addresses pre-decided for all C programs or something ?
    The textbook definition of a process is "an instance of a program in memory". If you run the same program twice, it will create two distinct processes.

    I see you still haven't done any serious reading on operating system. I suggested you do just that a while ago...
    Last edited by Bachstelze; January 13th, 2013 at 09:36 PM.
    「明後日の夕方には帰ってるからね。」


  8. #8
    Join Date
    Jun 2010
    Beans
    92

    Re: how to write to a particular address in C?

    Quote Originally Posted by IAMTubby View Post
    Hello,
    I am trying to write to a specific memory location in C, but I keep getting a segmentation fault.
    All the addresses returned are in 7digit hexadecimal, and the address used in the below program (85a9008) was got printing the address from a previous program, which means that writing to this memory location is permitted.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
      char* address = (int *)0x85a9008;
      *address = 'a';
      printf("\n*address == [%c]",*address);
      printf("Memory address is: 0x%p\n", address);
    
      return 0;
    }
    Please advise.
    Thanks.

    You will have to write a device driver.

    If the address is allocated memory to your application, your application will communicate this virtual address to your driver. The driver will perform the translation of the virtual address, and then do the actual accesses. The driver will then make the results available to your application.

    If the address is not allocated memory but is memory-mapped hardware, such as a register of a peripheral device (video, hard-drive, etc) then the driver will first have to get permission to access the physical address.

    The only exceptions to this that I can think of (in Linux) are certain legacy addresses that allow access from applications using ioperm(). These are things such as legacy serial ports, etc.

  9. #9
    Join Date
    Dec 2012
    Beans
    24

    Re: how to write to a particular address in C?

    What you want to read up on are TLBs and page tables

  10. #10
    Join Date
    Dec 2010
    Beans
    13

    Re: how to write to a particular address in C?

    I agree with xb12x, to have access a reserved address you will have to write a driver. After that you register drivers on kernel and you will be able to use it from user space.

    To use the address in the driver, look for mmap() implementation. It is a start point

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •