-3

In Windows, one can use structured exception handling in C to write a pseudo-loop that prints all numbers from 1 to 1000 like this:

int n = 0;
__try {
    *(int *)0 = 0;
}
__except(printf("%i\n", ++n), n < 1000 ? -1 : 1) {
}

I wonder if there are also other ways in C/C++ to create a loop that isn't trivial to detect if you search the code for the usual suspect keywords for, while and goto.

GOTO 0
  • 42,323
  • 22
  • 125
  • 158
  • 3
    Function calls (recursion) or any flow control mechanism can be used to do this. – Seth Carnegie Jan 09 '13 at 16:07
  • A simple `#define for loop` will protect against simple grepping for the keyword. Of course, it won't be effective against anybody with an IQ higher than 20, but neither will your __try/__except. – Jerry Coffin Jan 09 '13 at 16:08
  • 2
    Does `setjmp`/`longjmp` count? Or recursion... 1000 is well within the range of what fits on a stack :-) – Damon Jan 09 '13 at 16:55
  • @ft1: you can use assembly code as indicated in the following question http://stackoverflow.com/questions/14523333/compare-2-numbers-with-assembly – MOHAMED Jan 25 '13 at 16:03

1 Answers1

2

In C++, a simple lambda can do that:

std::function<void(int,int)> print = [&](int from, int to)
{
    std::cout << from << " ";
    if ( from < to ) print(++from, to);
};

print(1, 1000);

It will print all integers from 1 to 1000. And it doesn't use for, while or goto.

Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • thanks but this uses recursion, it's not the same as a loop – GOTO 0 Jan 09 '13 at 16:16
  • 4
    @ft1 why not. You need to define "loop", then. (IOW, thanks but your solution uses SEH, it's not the same as a loop) – R. Martinho Fernandes Jan 09 '13 at 16:17
  • @R.MartinhoFernandes practically, this would cause a stack overflow for too many iterations, whereas a loop does not – GOTO 0 Jan 09 '13 at 16:19
  • @ft1: You said `1` to `1000`. *I think* it will not cause stack overflow for that, as it doesn't consume too much stack memory. BTW, it is the basic idea which *can be made* to work for any size of input. – Nawaz Jan 09 '13 at 16:20
  • @Nawaz that was just an example, what I'd like to know is if there are other general solutions – GOTO 0 Jan 09 '13 at 16:22
  • @ft1: even this is an example which demonstrates a basic idea which *can be made* to work for any size of input, as I said in my previous comment. – Nawaz Jan 09 '13 at 16:23
  • @ft1: BTW, why do you want to know general solutions for such impractical problems? don't waste your time on such questions. it is fine when you do it for fun sake, but don't go too far with it. – Nawaz Jan 09 '13 at 16:24
  • 3
    @ft1 Tail recursion is equivalent to a loop. – Cat Plus Plus Jan 09 '13 at 16:26
  • @CatPlusPlus not really, as I said already, this type of recursion can cause a stack overflow because a new stack frame is created before the current one is reset. – GOTO 0 Jan 09 '13 at 16:28
  • @ft1: For REAL problems, this type of recursion is FINE. – Nawaz Jan 09 '13 at 16:30
  • 1
    @ft1: Yes, really. Tail recursion is completely equivalent to and can be trivially transformed into a constant-space loop and all modern C++ compilers do that. – Cat Plus Plus Jan 09 '13 at 16:32
  • 2
    @ft1: WTF are you smoking? Tail recursion is exactly equivalent to a loop, and re-uses stack frames. – Puppy Jan 09 '13 at 16:32
  • @Nawaz never said the contrary. Can nobody here understand the point of my question? – GOTO 0 Jan 09 '13 at 16:38
  • 2
    @ft1 Perhaps if you explained the point. At the moment, it's just a fun, but irrelevant, question. – Daniel Fischer Jan 09 '13 at 16:39
  • @DanielFischer the point is obvious if you think of the enormous efforts that modern languages have made to prevent spaghetti programming, simplify iterations etc. – GOTO 0 Jan 09 '13 at 16:44
  • 3
    @ft1 I don't see the point, **why not just write a loop to loop?** – Daniel Fischer Jan 09 '13 at 16:46
  • @DanielFischer you can write a loop to loop, but will you find a loop if you search for a loop? – GOTO 0 Jan 09 '13 at 16:48
  • 4
    @ft1 What do you mean? If the code is decently written, grepping for `for` and `while` (and under some circumstances `goto`) will find the loops. Are you trying to find out in what ways a malevolent coder could obfuscate loops in order to find out what else to search for? – Daniel Fischer Jan 09 '13 at 16:51
  • @DanielFischer indeed, that could be a practical use case. – GOTO 0 Jan 09 '13 at 16:55
  • 5
    @ft1 It's as far from "practical" as you can get. – Cat Plus Plus Jan 09 '13 at 18:27