Thread: Accessing a certain Memory Location

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    8

    Accessing a certain Memory Location

    I have a memory address. I want to output the content of this memory address.how can I do this in C ?

  2. #2
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    Something like this:
    Code:
    volatile unsigned char *addr = (unsigned char *)0x80000000;
    Just because I don't care doesn't mean I don't understand.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    8
    Thank you.

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    8
    Could you possible give an explanation as to how this works? not sure.

  5. #5
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    It's pretty straightforward. You cast the address--as a literal value--to a pointer of suitable type for how you're going to use it. The pointer then points to that address and you can use it just like a pointer to any other memory. The only difference is that you should make the pointer volatile because it refers to memory outside of your control.
    Just because I don't care doesn't mean I don't understand.

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Code:
    #include <stdio.h>
    
    int main()
    {
    	volatile unsigned char *addr = (unsigned char *)0x80000000;
    	printf("%d\n", *addr);
    
    
    	return 0;
    }
    It doesn't work with 32-bit compilers on XP MS-Windows operating system because that memory address is not mapped into the program's address space. Don't know about *nix.
    Last edited by Ancient Dragon; 09-11-2005 at 03:08 PM.

  7. #7
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    Well naturally it's not portable. Presumably bhuwan already knows that it works on his system, or he'll be back shortly to ask why it doesn't work.
    Just because I don't care doesn't mean I don't understand.

  8. #8
    Registered User
    Join Date
    Sep 2005
    Posts
    8
    I shrunk the code down to:
    Code:
    int tmpaddr = X value; // input from user
    char *addr = (char *)tmpaddr;   // convert
    // then i printed the contents using printf
    I am on WIN XP and it worked fine. Thanks for your help.

  9. #9
    Registered User
    Join Date
    Sep 2005
    Posts
    8
    Quote Originally Posted by Narf
    Something like this:
    Code:
    volatile unsigned char *addr = (unsigned char *)0x80000000;
    Quote Originally Posted by Narf
    It's pretty straightforward. You cast the address--as a literal value--to a pointer of suitable type for how you're going to use it. The pointer then points to that address and you can use it just like a pointer to any other memory. The only difference is that you should make the pointer volatile because it refers to memory outside of your control.
    Ps I am not sure how the (char *) works
    is that simply saying convert the value to a char type pointer?
    Last edited by bhuwan; 09-11-2005 at 03:26 PM. Reason: added code

  10. #10
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by bhuwan
    I shrunk the code down to:
    Code:
    int tmpaddr = X value; // input from user
    char *addr = (char *)tmpaddr;   // convert
    // then i printed the contents using printf
    I am on WIN XP and it worked fine. Thanks for your help.

    I can get lots of crap to compile too, but it doesn't mean it will run. your code is not dereferencing the addr variable. Yes, it will compile, but you can't do anything with it. Try this, which crashes too.
    Code:
    int main()
    {
    	unsigned int X = 0x80000000;
    	volatile unsigned char *addr = (unsigned char *)X;
    	unsigned n = *addr;
    	printf("%d\n", n);
    
    
    	return 0;
    }
    Last edited by Ancient Dragon; 09-11-2005 at 03:46 PM.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > I have a memory address.
    How about starting at the beginning, by stating what it is you actually hope to achieve with this.
    Because very few machines will tolerate you picking out random memory locations and then going and looking to see what's there.

    Don't forget to mention what your real operating system and compiler is.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Registered User
    Join Date
    Sep 2005
    Posts
    8
    Sorry for the confusion but i've gotten the task i wanted to do to work properly. All I am asking is this:

    Ps I am not sure how the (char *) works
    is that simply saying convert the value to a char type pointer?

  13. #13
    Registered User
    Join Date
    Sep 2005
    Posts
    8
    Quote Originally Posted by Ancient Dragon
    I can get lots of crap to compile too, but it doesn't mean it will run. your code is not dereferencing the addr variable. Yes, it will compile, but you can't do anything with it. Try this, which crashes too.
    Code:
    int main()
    {
    	unsigned int X = 0x80000000;
    	volatile unsigned char *addr = (unsigned char *)X;
    	unsigned n = *addr;
    	printf("%d\n", n);
    
    
    	return 0;
    }
    Thanks for your help, but I should have said it more claerly above: what I wanted to do has worked out fine. But again thanks fory our help

  14. #14
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by bhuwan
    Ps I am not sure how the (char *) works
    is that simply saying convert the value to a char type pointer?
    its called typcasting -- telling the compiler to redefined an object from one type to another. Otherwise, it does nothing, except sometimes hide errors.

  15. #15
    Registered User
    Join Date
    Sep 2005
    Posts
    8
    Quote Originally Posted by Ancient Dragon
    its called typcasting -- telling the compiler to redefined an object from one type to another. Otherwise, it does nothing, except sometimes hide errors.
    What exactly is the "CHAR *"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accessing variables with memory address
    By ITAmember in forum C Programming
    Replies: 54
    Last Post: 06-28-2009, 03:35 AM
  2. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  3. allocate apecified memory location for a c variable
    By BharathKumar in forum Linux Programming
    Replies: 5
    Last Post: 06-01-2007, 03:47 PM
  4. accessing windows (98) memory from a dos box?
    By thedumbmutt in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 05-12-2003, 12:50 PM
  5. Accessing memory directly
    By YALINI in forum C Programming
    Replies: 0
    Last Post: 08-30-2001, 11:56 PM