How do I "hack" my own programs?

For example, let's say I wrote a simple program:

#include <iostream>

int main()
int i = 0;

return 0;

How would I chage the variable i into 2 during runtime without having the source code/knowledge of the variable even existed? Or is that even possible in this example?

Last edited on
1) disassemble your program, replace needed part with jump, do your manipulations and jump back.
2) Pause your process, find address of your variable and write anything you want in it.

3) your concrete example is un"hack"able because your assigment is simply optimized away.
What do you mean it a optimised away?
The compiler recognizes that i is never used, therefore the statement at line 5 serves no purpose and is removed by the optimizer.
If I made the integer a pointer, would it still be deleted by the program while it was running?
The optimizer runs at compile time, not run time.
And yes, if the optimizer determines that the pointer is not used, it would also be removed by the optimizer.
Is there something you can do to tell the compiler not to optimize i like using "volatile" or something similar?
Last edited on
Yes. Check your compiler documentation for optimization options.
Ok. So let's say I print i to the screen. So now it's being used and shouldn't be deleted, right?

How do I do step 1 and two from there?
closed account (j3Rz8vqX)
Prior to step 1:

It will inform you of the necessaries.
So now it's being used and shouldn't be deleted, right?

That depends on your compiler and hardware. The compiler might choose to place i in a register and still might not allocate storage for it.

i know this might be tedious, but how would i make certain it's in memory?
int *i = new int(0); should allocate it on the heap.

Also I think to hack it you have to disassemble then dll inject it.
i will not be optomised away if you don't run g++ with optomizer flags.
Last edited on
Topic archived. No new replies allowed.