Why won't this work?

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
56
57
58
59
#include <iostream>
#include <exception>
#include <stdexcept>
#include <string>

using namespace std;
//const int dat = 10;
int dat = 10;

bool test(){cout << "dat = " << dat << endl; cout << "&dat = " << &dat << ".\n" << flush << endl; }
bool tested = test();

bool initialize(){srand(time(0));}
bool initialized = initialize;

template <class type>
class mem
{
    public:
    int ta;
    type * data;
    mem(){int ta; system("Pause");cin >> ta; data = ta; if(erase()) cover_tracks(); return;}
    mem(const int & d){data = &d;}
    ~mem(){}

    bool erase()
        {
            type c = *data;
            for (int i = 0; i < 10000; ++i)
                {
                    *data = (type)rand();
                }
            if (c == *data)
                return false;
            else return true;
        }

    bool cover_tracks()
        {
            *data = 700;
        }
};mem<int>ram;
bool stealth() {/*mem<int>ram;*/ exit(789);}
bool didwork()
{
    if (*ram.data != 700) return false;
    if (&ram.ta != ram.data) return false;
    return true;
}
bool worked = didwork();
bool hide_n_sneak = worked ? stealth():false;

int main()
{
    cout << "Hello World!\n" << flush;
    while(1)cout << "dat = " << dat << ".\n" << flush << endl;
    return 0;
}


It changes dat to 700, but main will still execute despite exit(789)ing from function stealth().

Also, it won't work when I cin the address in of the variable of dat in the following program:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;

int main()
{
    int secure_data = 10;
    cout << "&secure_data = " << &secure_data << ".\n" << flush << endl;
    cout << "secure_data = " << secure_data << ".\n" << flush << endl;
    system("Pause");
    while(1)cout << "secure_data = " << secure_data << ".\n" << flush;;
    return 0;
}


So if I run the latter, and then type in the decimal equivalant of the address of secure_data in the former program, and then resume the latter, the value in secure_data remains 10.

But it is supposed to change to 700, just like in the other program. Why is this?
The order of initialization of global data is undefined. For example, worked could be initialized after hide_n_sneak.

So if I run the latter, and then type in the decimal equivalant of the address of secure_data in the former program, and then resume the latter, the value in secure_data remains 10.

But it is supposed to change to 700, just like in the other program. Why is this?
Modern operating systems use virtual memory. Each process is put in an address space of its own, such that the address 0xDEADBEEF can translate to the physical address 0x00C0FFEE in one process and to 0x0CAB005E in another. This completely prevents one process from accidentally (or purposely, in your case) overwriting another process' memory.
Last edited on
Also: bool test(){cout << "dat = " << dat << endl; cout << "&dat = " << &dat << ".\n" << flush << endl; }

This doesn't return a bool...or anything for that matter.
I am probably wrong but bool Test () does not return a value.
And so is bool initialize()
Sorry if I said something stupid.
Edit: Oh sorry someone already said that.
Last edited on
it doesn't need to. its just a lot more convenient than to execute a void function b4 main.
so how would I get the physical address, so that i can overwrite the memory of another program?
The OS probably has system calls that let you access another process' memory. I know Windows does.
As far as I know, no system will give physical addresses to user processes. It wouldn't do any good, anyway, since there's no way for a user process to access physical memory.
Topic archived. No new replies allowed.