3

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?

Angelos Chalaris
  • 6,611
  • 8
  • 49
  • 75

2 Answers2

3

"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.

Cedric Mamo
  • 1,724
  • 2
  • 18
  • 33
  • If he starts his program at the second cell instead and uses cell 1 as a failsafe with value 0, he can employ [<] however many times he wishes/needs to return his code to array 1 and no further. – Jouster500 Mar 21 '16 at 03:57
  • yeah but if, for example, he uses two cells to store a 16 bit number then any number smaller than 256 will mess that up. And a lot of other cases too. turns out in programming the number 0 is the one you use the most, especially when looping in BF – Cedric Mamo Mar 21 '16 at 18:31
  • In which it all goes back to watching what you're doing when programming. – Jouster500 Mar 21 '16 at 18:35
  • That is right, because the only control flow method in Brainf*** is loops (`[]`). – Obinna Nwakwue Apr 05 '16 at 22:03
3

Start your code with a > to make the first cell=0. Now assuming that the numbers in between are not 0's you can use [<] to return the starting array. So for example, i have explicitly mapped the ASCII characters A-Z to cells 2-27, Leaving Cell 1 as 0. This way, if i wanted to print out a message, i would go

> >>>>>>>>.>.[<] which displays hi before returning the data pointer to cell 1, saving myself the time of writing all those <.

Again, this is assuming that the numbers in between are not 0's. However if you were handling data in particular cells and you know that data wont ever reach 0, you can jump back to previous cell that was labeled 0, and so on. This is more or less like reverse gotos in batchfiles.

So lets say i have cell data [0 2 3 4 0 5 3 9 3 2 0 2 3 2 0] and we have the datapointer at the 3rd 0. We can jump back to the first array by doing <[<]<[<] as the command will skip the 0 value, and repeat [<] until it reaches the next 0. You can employ the same idea using the reverse if you need to quickly get the data pointer around your code. On top of that, a test run of the following code >+++>++>+[<][<][<] returned me to the first cell despite the additional < because the brackets forced the code to skip as cell 1 is labeled 0. (I tested it using this resource)

However as @Cedric Mamo said in his answer, there is no explicit way you can prevent the code from going out of bounds because if you accidentally typed < after you reached cell 1, you go out of bounds. Unless you program in the safeguards such as what i mentioned above and watch what you are doing, there is no exact way to prevent this.

Jouster500
  • 762
  • 12
  • 25