Thread: Accessing variables with memory address

  1. #1
    Registered User ITAmember's Avatar
    Join Date
    Mar 2009
    Location
    Nebraska
    Posts
    72

    Accessing variables with memory address

    Let's say I have an int at address 0x5FA70. I have another int storing that variable. How do I use that variable to manipulate the data at the memory address stored by that variable?

    Code:
    /* this is the variable i want to access. it's memory address is 
    0x5FA70. the kicker is I cannot use this variable in any way to 
    access it's value, only that memory address */
    int a = 645;
    
    /* this variable stores 0x5FA70, the address of a */
    int b = 0x5FA70;
    Let's say I want to add 1 to a, then I want to assign the value of a to a new int, c. How would I do this without using the variable a in any way and only using it's address? Thanks in advance.
    Last edited by ITAmember; 06-23-2009 at 05:01 PM. Reason: word wrap the comments

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Do you know what * does when you place it in front of a variable?

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Code:
    volatile unsigned int* const b = (unsigned int *)0x5FA70;

  4. #4
    Registered User ITAmember's Avatar
    Join Date
    Mar 2009
    Location
    Nebraska
    Posts
    72
    Quote Originally Posted by tabstop View Post
    Do you know what * does when you place it in front of a variable?
    Ah, but what if a is a double, struct, float, etc?

    Quote Originally Posted by bithub
    Code:
    volatile unsigned int* const b = (unsigned int *)0x5FA70;
    The big question is, how fast is that?

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by ITAmember View Post
    Ah, but what if a is a double, struct, float, etc?
    Why would it matter? That's not really relevant, so far as I can see (I mean, you have to know what type it is, but how you deal with the indirection doesn't change).

    Quote Originally Posted by ITAmember View Post
    The big question is, how fast is that?
    It's loading a constant into a memory location. I don't know that there are any operations that take less time. (Now if you're asking about speed of using b, that's something else.)

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    The big question is, how fast is that?
    You are declaring a pointer and initializing it to a value. I'd say it's pretty fast. The only valid reason I can think of for doing something like this is if you are working on a system that has memory-mapped addresses. Otherwise, you are probably doing something unnecessary and dangerous.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Fast compared to what?

    If the value is in a register, it will be faster than accessing it through a pointer.

    And with regards to "other types", you have to know what the type is of the variable. [And of course, it's not very maintainable to acess variables at fixed addresses in memory. It only take a different compiler or a change in compiler options for the variable to move to another address].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User ITAmember's Avatar
    Join Date
    Mar 2009
    Location
    Nebraska
    Posts
    72
    When I said "how fast is that?" I was referring to the (unsigned int *). I'm guessing it's just something to tell the compiler how to generate the code and doesn't actually use any instructions.

    I was using the fixed address for demonstrations purposes, in the real program I'll use b = &a; to get the address.

  9. #9
    Registered User ITAmember's Avatar
    Join Date
    Mar 2009
    Location
    Nebraska
    Posts
    72
    My initial tests. My goal is to access the type member of the dynamic_int struct instance "test1". So far it does not work. I'm using Visual C++ 2008 Express Edition on a P4.

    Code:
     #include <stdio.h>
    
    /*define a dynamic int. a dynamic object will hold the mem address of the struct holding
    the current value of that dyanmic object*/
    typedef struct
    {
    	int type;
    	int data;
    } dynamic_int;
    
    int main()
    {
    	/*create a dynamic integer for tests*/
    	dynamic_int test1;
    	/* initalize the data members */
    	test1.type = 0;
    	test1.data = 1;
    	/*print the memory addresses for the struct, the type member, and the data member*/
    	printf("test1 %#x, type %#x, data %#x\n", &test1, &(test1.type), &(test1.data));
    	/*try the print the value of the type member, then print the address of that member*/
    	printf("test1.type = %i, address = %#x\n", (int *)((&test1)), (&test1));
    	return 0;
    }

  10. #10
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> My goal is to access the type member of the dynamic_int struct instance "test1". So far it does not work.

    Code:
     #include <stdio.h>
    
    /*define a dynamic int. a dynamic object will hold the mem address of the struct holding
    the current value of that dyanmic object*/
    typedef struct
    {
    	int type;
    	int data;
    } dynamic_int;
    
    int main()
    {
    	/*create a dynamic integer for tests*/
    	dynamic_int test1;
    	/* initalize the data members */
    	test1.type = 1024;
    	test1.data = 1;
    	/*print the memory addresses for the struct, the type member, and the data member*/
    	printf("test1 %#x, test1.type %#x, test1.data %#x\n", &test1, &(test1.type), &(test1.data));
    	/*try the print the value of the type member, then print the address of that member*/
    	printf("test1.type = %i, address = %#x\n", *(int *)&test1, &test1);
    	return 0;
    }
    But keep in mind that due to potential structure padding, the member you are trying to access (except for the first) may not be where you think it is (use the offsetof macro or an alignment compiler directive to remedy this).

    At any rate, if you knew the address of a variable you could simply do:

    Code:
    *(int*)0x5FA70 = 1024;
    Not a very good idea, of course.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  11. #11
    Registered User ITAmember's Avatar
    Join Date
    Mar 2009
    Location
    Nebraska
    Posts
    72
    Here's the output

    Code:
    test1 0x12ff5c, type 0x12ff5c, data 0x12ff60
    test1.type = 1245020, address = 0x12ff5c
    The address for test1.type in the second line is the same as "type" in the first line. Two integers accessing the exact same memory, why do they have different values?

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by ITAmember View Post
    Here's the output

    Code:
    test1 0x12ff5c, type 0x12ff5c, data 0x12ff60
    test1.type = 1245020, address = 0x12ff5c
    The address for test1.type in the second line is the same as "type" in the first line. Two integers accessing the exact same memory, why do they have different values?
    They don't. (Note that none of the values on the first line are values, they are all addresses.)

  13. #13
    Registered User ITAmember's Avatar
    Join Date
    Mar 2009
    Location
    Nebraska
    Posts
    72
    When I print test1.type it comes out as 0.

  14. #14
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    If you're using the code I posted, you should get 1024 for test.type. AFAIK, the first member of the structure is guaranteed to have the same base address as the structure (and even your output verifies this), and so there is no way you'd get that result.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  15. #15
    Registered User ITAmember's Avatar
    Join Date
    Mar 2009
    Location
    Nebraska
    Posts
    72
    Are you saying there is no way for me to get 0 to print? I'm kind of confused now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutex and Shared Memory Segment Questions.
    By MadDog in forum Linux Programming
    Replies: 14
    Last Post: 06-20-2010, 04:04 AM
  2. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  3. Printing out the memory address
    By Scalpel78 in forum C++ Programming
    Replies: 16
    Last Post: 01-09-2007, 11:01 AM
  4. Changing memory address
    By tzpb8 in forum C Programming
    Replies: 3
    Last Post: 07-26-2006, 09:50 AM
  5. Pointer to specific memory address
    By elnerdo in forum C++ Programming
    Replies: 10
    Last Post: 05-19-2006, 07:35 AM