Posted 15 December 2018 - 07:50 AM
I have this C code:
It terminates, as expected.
I have this Java code:
It does not terminate.
Why do they behave differently?
#include <stdlib.h>
#include <stdio.h>
typedef unsigned char u8;
void m1();
void m2();
u8 *dp;
int main() {
dp = (u8*) calloc(30000, sizeof(u8));
dp += 1;
*dp -= 1;
while(*dp) {
m1();
}
return 0;
}
void m1() {
while(*dp) {
m2();
}
dp += 1;
}
void m2() {
dp -= 1;
*dp += 1;
dp += 3;
*dp -= 1;
dp -= 1;
*dp -= 1;
dp -= 1;
*dp += 1;
}
It terminates, as expected.
I have this Java code:
public class test {
public static void main(String[] args) {
new test().run();
}
int[] tape = new int[30000];
int dp = 0;
void adjust(int n) {
tape[dp] += n;
if(tape[dp] < 0) tape[dp] += 255;
if(tape[dp] > 255) tape[dp] -= 255;
}
void run() {
dp += 1;
adjust(-1);
while(tape[dp] != 0) {
m1();
}
}
void m1() {
while(tape[dp] != 0) {
m2();
}
dp += 1;
}
void m2() {
dp -= 1;
adjust(1);
dp += 3;
adjust(-1);
dp -= 1;
adjust(-1);
dp -= 1;
adjust(1);
}
}
It does not terminate.
Why do they behave differently?
Edited on 15 December 2018 - 07:21 AM