Change value with pointer

So what I am trying to do is pull the address from a variable then assign this address to another pointer in another program and then modify the value at this memory location and then have the other program spit out the new results. I set this up by first creating a program that first prints out the address, then the original value. You then can press 2 and it will display the variable again to see if the value changed. I set up a test case to see if it has changed but it does not work outside of the program!

If you want to try it yourself the directions are to:

run the first program below which gives the address and the value, You then modify the second program by entering in the address given by the first program and compile it and run it. You then go back to the first program(still running) and type 2 and press enter and it should determine if the integer has been modified or not...


Please note my focus here was not on program design


Any help would be great!


First Program
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>

using namespace std;

int main()
{
    int response;

    while (true)
    {
        int toChange = 15;
        cout << "The address of toChange is equal to " << &toChange << endl;
        cout << "The value of toChange is currently " << toChange << endl;
        cin >> response;

        if (response == 1)
            break;

        else if (response == 2)
        {
            cout << "Checking the value you now, it is " << toChange << endl;

            if (toChange != 15)
                cout << "\nHoly shit! It worked!\n";
            else
                cout <<"\nNope did not work.... " << endl;


            cin >> response;




            if (response == 1)
                break;
            else
            {
                continue;
                response = 0; //TO AVOID ANY WEIRD SHIT

            }

        }

    }

    cout << "Program Ended" << endl;

    return 0;
}  










2nd program

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
  #include <iostream>

//THIS PROGRAM WILL CHANGE THE POINTER

using namespace std;

int main()
{

   int *ptr;  // Declare pointer
   ptr = reinterpret_cast<int*>(0x28fef8);

   //should send address to the console
   cout << ptr << endl;
   *ptr = 20;
   cout << "Done with mod to pointer!"<< endl;
   cout << "New pointer should point to value of " << *ptr << endl;

   while (true)
   {
       int answer;
       cout << "to make the program stop running type 1" << endl;
       cin >> answer;

       if (answer == 1)
        break;

   }




    return 0;
}



First program:
-There is no pointer and there is no changing of the toChange's value.
-In your last else statement, you move on to the next iteration before changing the value of response, so line 39 would never execute.

Second program:
-Using some random memory address is precarious as you have no idea what is supposed to be there. Just declare a dummy variable and use its memory address during the test. At least you know you won't be screwing something over.
-On line 15, you decided to assign 20 to that arbitrary memory location. Again, this is precarious. Who knows what might happen to that location and what programs actually use it. I believe this counts as a security hole in your program.
Last edited on
ptr = reinterpret_cast<int*>(0x28fef8);
IS NOT A GOOD SOLUTION
a better solution would be to write the adress &toChange in a file and make the 2nd program read the file,
because it is possible that every time you start the program
&toChange changes

if you make a program, this program will get a part of your RAM, and if you try to use an adress outside your part, your program may crash

it is better that 2nd program writes a value in a file, and the 1st program reads this file then
Thank you all for the reply! Been awhile since I checked back since this went unanswered for awhile!
Topic archived. No new replies allowed.