Server Help

Trash Talk - Programming problem

Mr Ekted - Wed Nov 10, 2004 12:45 pm
Post subject: Programming problem
State 3 ways that the following code can ever exit. Crashing or terminating the process does not count. If this is too easy for you, don't give away the answers. Let others try.

Code: Show/Hide
a = 1;

while (a)
   b++;

// how do we get here?

CypherJF - Wed Nov 10, 2004 1:01 pm
Post subject:
<comment>Nice post #666. icon_smile.gif</comment>
Bak - Wed Nov 10, 2004 5:31 pm
Post subject:
(answers attached so as to not ruin problem)
Mr Ekted - Wed Nov 10, 2004 11:21 pm
Post subject:
Good but there are more.
tansey - Thu Nov 11, 2004 9:27 pm
Post subject:
besides the 3 thrown up there, couldn't you also overload the class of a's '=' operator to set it to 0 if rvalue is 1?
Bak - Thu Nov 11, 2004 11:50 pm
Post subject:
perhaps, but that would be operator overloading again. I wonder what function the while(a) would call... does it do a != 0 so operator!= or perhaps it does a cast to a bool... if so than what would it do in c... where there are no bool's (integer cast?). Ekted probably would know.

Similarly you could use #define in a few different ways to get the desired result.

A different solution would be to start a different thread that does

while (1)
a = 0;

forever and that should do it... assuming a is a global variable... unfortunately I can't seem to find a simple c++ example showing multithreading, so I don't know exactly what the syntax would be... I guess it's the idea that counts though.
50% Packetloss - Fri Nov 12, 2004 1:49 am
Post subject:
#define while(a) while(0)
#define b++ break

Good game sons!!

Edit: Damn, didnt read your post, guess you already found these answers
Mr Ekted - Fri Nov 12, 2004 9:19 am
Post subject:
Clue:

volatile int a;
SuSE - Fri Nov 12, 2004 12:11 pm
Post subject:
I know!
Code: Show/Hide
<html>
   <form>
      <input type crash>
   </form>
</html>

50% Packetloss - Fri Nov 12, 2004 4:24 pm
Post subject:
You could use __asm to have the program self-modify itself. Maybe even have another thread edit the value of a while the program is in the loop, I dont know enough about multi-threading though. I tried looking up the volatile stuff and its beyond me.
Bak - Sun Nov 14, 2004 3:36 pm
Post subject:
volatile looks like it prevents the comipler from optimizing certain values by having it always read them from memory, rather than store in registers. Using volatile values and multithreading it looks like we could change a in different thread

Code: Show/Hide
volatile int a;

ThreadFunc()
{
  while(1)
    a = 0;
}

int main()
{
  int b;
  startThread(&ThreadFunc);

  a = 1;

  while (a)
     b++;

  return 0;
}

again, the exact syntax is beyond me. use AfxBeginThread or something like that if you use MFC and #include <windows.h>.

The assembly self modifing solution is a funny one 50%.
Mr Ekted - Sun Nov 14, 2004 8:45 pm
Post subject:
Good work everyone. This concludes the exercise.
D1st0rt - Sun Nov 14, 2004 10:49 pm
Post subject:
couldn't you just step over it in a debugger?
1stStrike - Sun Nov 14, 2004 10:54 pm
Post subject:
psh. programming! who needs programming anyway? software is for the weak! tongue.gif
Cyan~Fire - Sun Nov 14, 2004 11:04 pm
Post subject:
D1s, I don't think you quite understand the debugger commands. You can't just skip a section of code, that would cause quite a few problems and wouldn't be too useful. The only call it skipping because it's going too fast to see. icon_razz.gif (My wacko explanation)
D1st0rt - Sun Nov 14, 2004 11:06 pm
Post subject:
ok, I really have no knowledge of debuggers since nothing I've done has been complex enough to require serious use... I just saw the "step over" button in VS
Mr Ekted - Mon Nov 15, 2004 3:12 am
Post subject:
Cyan~Fire wrote:
D1s, I don't think you quite understand the debugger commands. You can't just skip a section of code, that would cause quite a few problems and wouldn't be too useful. The only call it skipping because it's going too fast to see. icon_razz.gif (My wacko explanation)


Yuo can change the value of EIP any time you like, as long as you know what you are doing. This effectively performs a JMP.
Dr Brain - Mon Nov 15, 2004 7:15 am
Post subject:
In assembly debuggers, you can change the PC register and that will skip commands. I'm not sure I've ever seen that functionality on a 8086 debugger, but it's probably there.

But you can do it on some embedded system debuggers, because they give you the ability to manually change all of the registers.
Mr Ekted - Mon Nov 15, 2004 9:52 am
Post subject:
EIP is the program counter for Intel. In VC you can edit registers simply by typing new values for them.
Dr Brain - Mon Nov 15, 2004 10:12 am
Post subject:
Oh? Interesting. I've been working mostly with Motorola chips thus far.

Never really used Microsoft's debugger, either.
Purge - Mon Nov 15, 2004 3:18 pm
Post subject:
Damn smart people... icon_confused.gif
All times are -5 GMT
View topic
Powered by phpBB 2.0 .0.11 © 2001 phpBB Group