Angelos Chalaris Angelos Chalaris - 1 month ago 8
Brainfuck Question

Pointer position - go to 0 or specific (check if out of bounds)

I am a newbie in brainfuck and just starting to take my first steps. I would like some help figuring out a clever way (even if it is a bit hacky) to see if my pointer is out of bounds, before it goes there. Therefore I would like to be able to see that I am at the first cell of the memory array and not decrease my pointer any further. I tried googling this and I couldn't find anything. If anyone has any ideas, I would appreciate them even if they are just pure logic and no code.

From what it seems many brainfuck interpreters don't get stuck if someone writes a piece of code like

<<<<.
as they just wrap around at the end of the memory array. However, some of them get stuck. So, if this is a very interpreter-specific question, let me generalize a bit: Is there a way to go to a specific position in the memory array regardless of existing position?

Answer

"Is there a way to go to a specific position in the memory array regardless of existing position?"

Very simple answer: no.

I'm not trying to be a smart*ss or anything. The answer really is that short and simple. That is simply not possible in BF. You want to ensure you don't go out of bounds? You'd have to code properly, by making sure that you can always tell where the pointer actually is.

And even if you write code that happens to work well on one interpreter using a "hacky solution", then it wouldn't really be a valid program. It wouldn't run on just any interpreter.

In BF the cells aren't addressable. You have to live with that fact.

Comments