Editting Memory Addresses?

This is a discussion on Editting Memory Addresses? within the C++ Programming forums, part of the General Programming Boards category; I was wondering how I could modify memory addresses. ie. I've been creating a static int, printing the memory address, ...

  1. #1
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034

    Editting Memory Addresses?

    I was wondering how I could modify memory addresses.

    ie. I've been creating a static int, printing the memory address, then going back to the program and seeing if I can assign another int that memory address.

    Code:
    int main()
    {
      int Init;
      int anInt;
    
      cout << &Init;
      &anInt = *address_of_Init_here; //ie. 0x22ff7c
    
      cin.get();
    }
    I was wondering because the free store (memory thats allocated until deallocated: new/delete) is supposedly non-accessible from outside the function its declared in.. but would this get around it?

    However I get the normal error: non-lvalue in assignment (and if I dont get that, I get a invalid conversion error).

    Also, anyone know if you can modify memory addresses from outside the program using them? I'm going to assume you can, if theres a way around non-lvalue error. So would allocating to the free store prevent this?
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,259
    hmm... could you be looking for something along these lines?
    Code:
    int a = 1;
    cout << "a: " << a << " @ " << &a << endl; 
    int b = 2;
    cout << "b: " << b << " @ " << &b << endl;
    //create pointer to point to address of a
    int* ptr = &a;
    //assign value of b to memory location at address pointed to by ptr
    *ptr = b;
    cout << "a: " << a << " @ " << &a << endl;
    EDIT:
    No, I dont think it does what you want. It just does a normal assignment, effectively (i.e. my comment is wrong, since I have dereferenced the pointer). Oh well.
    Last edited by laserlight; 07-03-2005 at 12:20 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Yah, no, thats not what I'm looking for. I know about pointers, references, and assigned memory addresses of course.

    But what if I was trying to edit a memory address that is not run from my program, and hence would not have a variable in my program assigned to it.. hence cant say &a = &b, I'd have to say &a = 0x385sd3 (or whatever memory address it is). Is this possible? is what I was wondering.

    I dont think it is now, I've never heard of it.. you have to edit the address at the source of the packet? if not the hex # in the actual program. I was curious if it was possible because allocating memory to the heap/free store/stack have different effects, and I wondered if those effects applied to accessing the memory location from outside the program also.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    You can never change the adress of any variable declared anywhere. You can only change pointer values, which themselves are also variables which have their own memory position.

  5. #5
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by xErath
    You can never change the adress of any variable declared anywhere. You can only change pointer values, which themselves are also variables which have their own memory position.
    Ah crap, yeah I overlooked that. I originally was testing with a pointer but for this thread used a normal int, which you're right, you cant change.

    So what about a pointer? it still gives an error when assigning the memory address:

    invalid conversion from `int' to `int*'
    Code:
      int* aPointer = new int;
      int* aPointer2 = new int;
      
      *aPointer = 5;
      
      cout << aPointer << " " << aPointer2 << endl; //used to find memory address
    
      aPointer2 = 0x3e2928;
      cout << aPointer2; //used to test if it succeeded
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  6. #6
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    The compiler is telling you what is wrong...
    You assigning a int somewhere to a pointer. You should first the the adress of that int through the & operator and them assign the adress to the pointer. Or build a new int() with the value you provide. new int(int value = 0) allocates space from the heap enough to hold a int var.

  7. #7
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    I was wondering because the free store (memory thats allocated until deallocated: new/delete) is supposedly non-accessible from outside the function its declared in..
    Think you're confused here. It's stack variables that can't be accessed outside the functions they're declared in, because they get destroyed when the function exits. Variables allocated on the free store persist, so as long as you somehow retain a pointer to the variable you create, you can always access the variable later.

    >>aPointer2 = 0x3e2928;
    I'm not surprised you get an error. You shouldn't be doing this, first of all, because you're hardcoding some memory address. Anyway, if you *really* want to get around the compiler error (which I don't recommend), you might get away by typecasting it:
    aPointer2 = (int*)0x3e2928;

    If you're talking about modifying variables in other programs however, you'll need to use an API call such as WriteProcessMemory(). I've never used it before, so I don't know exactly how it works, but it seems to be your closest bet. You can look it up on MSDN.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  8. #8
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    I'm not trying to do anything ordinary xErath, I would never make a program trying to assign my own memory locations. I am doing it this way to test, and have fun.

    Thanks Hunter2, Dev-Cpp did let me get away with typecasting it:

    Code:
      int* aPointer = new int(5);
    
      //cout << aPointer << endl; //used to find memory address
      int* aPointer2 = (int*)0x3e2898; //assign aPointers memory address
    
      cout << *aPointer2; //used to test if it succeeded
    Yeah the stack is only accessible by that function, and the free store does persist but its still only accessible by that function.. Its with that I planned on testing. Since the variable is deleted after the function ends on the stack, I could not get the memory address and edit it from a different function, which is why I wanted to put it on the free store, since it would still exist after the function ends.. giving me the option to test if it will let me modify that option from outside the function.. and since the variable would be out of the scope, it was required that I use the memory address.

    So hence by using the memory address outside the function where the variable is declared on the free store, I am testing to see if the "cannot be used outside of the function" applies to the variable, or the memory address.

    If you understood what I just said.. great!

    Anyway finally, I found the result of my experiment. If you define a pointer to point to a memory address, it will try. If the memory address you define already is being used by something that is IN your program, it gives it a different memory address, overriding the memory address you specified. However if the memory address isnt in your program, it will use that address. So I had two programs using the same address for an int. The results were odd.

    I would have each program pausing in between printing the value/address and assigning a new value. Sure enough each program was using the same memory address, and setting its own values. They did not seem to share the same values, nor did defining a value on one make the value on the other program change. Almost as if I may have declared the pointer to point to that memory location, but some background routing by windows took place to relocate the address. Which could result in stray memory.

    Code:
    int aFunction() {
      int* aPointer = new int(5);
    
      cout << "aPointer: " << aPointer << endl; //find the memory address
      cout << "*aPointer: " << *aPointer << "\n" << endl; //testing the value
    }
    
    main()
    {
      aFunction();
    
      int* aPointer2 = (int*)0x3e2898; //assign aPointer's address here
    
      cout << "*aPointer2: " << *aPointer2 << endl; //returns 0
      *aPointer2 = 10;
      cout << "*aPointer2: " << *aPointer2 << endl;
      cout << "aPointer2: " << aPointer2 << endl;
    
      cin.get();
    }
    That was the first test, see the memory address 0x3e2898 would have been gone after calling aFunction() if it was not on the free store (because of new keyword). The result of this was aPointer2's pointed to address being relocated.

    The entire purpose of this was just to test and see if cracking a program from outside the program by altering memory address' was possible. It appears thats a negative. But maybe that WriteProcessMemory() can do it, I'm sure Microsoft knows how to write functions to bypass their own re-routing techniques used in Windows when reassigning the same memory address.

    Edit: BTW, the code I gave you here is the ENTIRE program, I'm just testing around with something... not giving you an error description.
    Last edited by Dae; 07-03-2005 at 08:12 PM.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how can know in memory someone addresses in free list
    By 0000000009 in forum C Programming
    Replies: 8
    Last Post: 10-17-2005, 01:35 AM
  2. help with displaying memory addresses
    By RancidWannaRiot in forum C++ Programming
    Replies: 7
    Last Post: 09-02-2005, 10:40 PM
  3. Memory allocation and deallocation
    By Micko in forum C++ Programming
    Replies: 3
    Last Post: 08-19-2005, 07:45 PM
  4. Pointer's
    By xlordt in forum C Programming
    Replies: 13
    Last Post: 10-14-2003, 03:15 PM
  5. Memory Addresses
    By Breetai in forum C++ Programming
    Replies: 6
    Last Post: 12-10-2002, 08:10 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21