Thread: Derive A Value At An Address

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    3

    Derive A Value At An Address

    Technically speaking, I'm aware of the fact that you can't dereference a void pointer.

    I decided to play around with pointers (I've been out of practice for a while) and I noticed something odd when I viewed the output of the following program:

    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char** argv)
    {
    	int i = 10;
    	cout << "Address of i " << &i << endl;
    
    	void *vPtr = &i;
    	cout << "Address of vPtr " << &vPtr << endl;
    	cout << "vPtr to i " << vPtr << endl;
    
    	system("PAUSE");
    	return 0;
    }
    The lines of code that print the "address of i" and the "vPtr to i" yield the same address. Now the thing that got me thinking was even though vPtr is a void pointer and can't be dereferenced, it can still access the memory address that holds the value of i. Wouldn't that mean that there would be some way to derive the value stored at that address though the void pointer? I had asked someone else about this and they told me that you'd have to get the actual byte size and you could infer the value from that.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    A void * does/can hold a legitimate memory address. But in order to interpret it in a meaningful way, you have to decide what is being pointed to. I.e.:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main() {
        int i = 48;
        void *vPtr = &i;
    
        int j = *((int *)vPtr);
        float k = *((float *)vPtr);
        char l = *((char *)vPtr);
        cout << "The pointer " << vPtr << " could be " << j << " or " << k << " or " << l << endl;
    
        return 0;
    }
    if you'll excuse the C-style casts, gives you perhaps some idea of what's going on. The data is there, but how you interpret the data is up to you.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    The lines of code that print the "address of i" and the "vPtr to i" yield the same address.
    I just wanted to point out that it does this because both lines are using the exact same thing (&i which is equal to vPtr).

    Now the thing that got me thinking was even though vPtr is a void pointer and can't be dereferenced, it can still access the memory address that holds the value of i.
    A "void*" can't be (directly) dereferenced because what it points to, or more specifically, the size of what it points to, is unknown. Of course you still have access to it's address. You can get it's contents (dereference it) by casting it to the proper type, in this case you'd do something like
    Code:
    int foo = *(int*)vPtr;
    This says 'get the value pointed to by the pointer vPtr, which is a pointer to an integer'.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    3
    Very interesting. Thanks a whole lot guys. That really helped out.

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. Writing array, to file
    By zootreeves in forum C Programming
    Replies: 9
    Last Post: 09-08-2007, 05:06 PM
  3. I thought pointers were pointers...
    By keira in forum C Programming
    Replies: 19
    Last Post: 08-15-2007, 11:48 PM
  4. DX - CreateDevice - D3DERR_INVALIDCALL
    By Tonto in forum Game Programming
    Replies: 3
    Last Post: 12-01-2006, 07:17 PM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM