Thread: Pointer to specific memory address

  1. #1
    Resident nerd elnerdo's Avatar
    Join Date
    Apr 2005
    Location
    Northern NJ
    Posts
    51

    Pointer to specific memory address

    Is it possible to have a pointer point to a SPECIFIC memory address? I want to be able to edit one certain address, and I'm not sure how to go about doing it.

    I would've assumed that something similar to

    Code:
    int* ptr;
    ptr = 0x83fc22;
    but it turns out that this is untrue.

    (Background info: I'm using devc++, I am a novice to the concept of pointers, but I am not a novice to programming. I generally program in .NET (vb.net and c#) and Java. )

    Do you know how or if this can be done?

    Also, another pointer question, while I'm at it: How can I get something's memory address into an int?

    As an example, let's say that I want to do something like this
    Code:
    int x;
    int y;
    y = &x; //Obviously, this won't work.  But, I want y to equal
    //something like '0x2fecd0'
    y += 5;
    //so that y can NOW equal 0x2fecd5
    cout << "Five more than the memory address of x is  "<< y;
    Last edited by elnerdo; 05-16-2006 at 08:08 PM.
    nerds unite!

    I'm using windows XP.
    I'm using dev-C++ by bloodshed.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    To make this work you can use a cast
    Code:
    int* ptr;
    ptr = (void *)0x83fc22;
    But this isn't really a good thing to do. You don't know what's at that addres or even if it lies within your address space.

    For your second question you can cast to an int

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Is it possible to have a pointer point to a SPECIFIC memory address?
    Given your current OS/Compiler, no.

    Programs operate in virtual memory, so the only memory you have is given to you by the OS. You can't just invent addresses and hope that something useful will happen.

    If you're writing device drivers, then obviously you need access to physical resources at some point, but that's pretty heavy stuff.
    Likewise, programs like debuggers can inspect and modify the program and data space of the program being debugged, but this is also in "voodoo" territory for most people.
    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.

  4. #4
    Resident nerd elnerdo's Avatar
    Join Date
    Apr 2005
    Location
    Northern NJ
    Posts
    51
    Thanks, you two. Quanatum, your solution did the trick. I guess I just wasn't thinking.

    Salem, I'll keep that in mind.
    nerds unite!

    I'm using windows XP.
    I'm using dev-C++ by bloodshed.

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by elnerdo
    Thanks, you two. Quanatum, your solution did the trick. .
    That solution only allowed it to compile -- doesn't mean it will work at runtime

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    34
    Just throwin' this in, although you say you've already solved your problem. Back in the day when I did graphics programming, I would use this to get a pointer to the 320x200 (8-bit color) vga screen, which is at 0A0000 in 32-bit protected mode:

    Code:
    unsigned char *vga=(unsigned char *)0x0A0000;

  7. #7
    char main() RoshanX's Avatar
    Join Date
    Mar 2003
    Posts
    68
    answer to your second question, since a pointer is an integer, it is possible to do it like this

    Code:
     y=(int)&x;
    First there was God. He was quite lonely, so he created Dennis.
    Dennis was unimpressed with God.
    So, . . . God created Brian..........Khan Klatt
    http://www.clifford.at/fun/GOD

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by RoshanX
    since a pointer is an integer, it is possible to do it like this
    Bad habit.
    http://c-faq.com/ptrs/int2ptr.html
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by RoshanX
    answer to your second question, since a pointer is an integer, it is possible to do it like this

    Code:
     y=(int)&x;
    won't work when sizeof(int) != sizeof(int *). So your code snippet is non-standard and probably produce unpredictable results with many compilers.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > since a pointer is an integer,
    Oops - back to school for you.
    The standard doesn't even guarantee that all pointers have the same size, never mind being the same size as an int.
    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.

  11. #11
    Registered User
    Join Date
    May 2006
    Posts
    3
    it is possible and dangerous that a pointer points a SPECIFIC memory address
    because may be this address is address of your graphic card or sound card etc... so this fact can spoil your pc (%1 chance )

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What does this do (Windows API)?
    By EVOEx in forum Windows Programming
    Replies: 4
    Last Post: 12-19-2008, 10:48 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. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  4. Memory Address
    By Stack Overflow in forum C Programming
    Replies: 5
    Last Post: 05-25-2004, 11:43 AM
  5. size of the memory reserved for a pointer
    By cris_orpi in forum C Programming
    Replies: 13
    Last Post: 03-05-2003, 11:42 AM