Temple of The Roguelike Forums

Development => Programming => Topic started by: Pueo on June 14, 2012, 04:29:37 AM

Title: Using -- and ++
Post by: Pueo on June 14, 2012, 04:29:37 AM
Quick question, when you use -- and ++ when accessing an array, is it also decrementing or incrementing the value?
(Example)
Code: [Select]
dungeon[row--][col][depth].connected = TRUE;
dungeon[row][col][depth].connections[dungeon[row][col][depth].numConnections][row--][col] = TRUE;
What I'm trying to do is access the section one row above (physically above, so the row has to decrease) the current row.

What the debugger says I'm doing is that the row variable (the one I'm decrementing) starts at 1 (before this code block starts), goes to 0 (after the first line), then goes to -2 (after the second line).  Then, of course, that means I'm trying to access an array with a negative value, which is producing a BAD_ACCESS error.
Title: Re: Using -- and ++
Post by: Hi on June 14, 2012, 05:37:14 AM
yes, use "row-1'

You should not be incrementing or decrementing your index variable, in the middle of indexing an array, without a very good reason.
Title: Re: Using -- and ++
Post by: Pueo on June 14, 2012, 06:13:04 AM
yes, use "row-1'
Alright, thanks.
Title: Re: Using -- and ++
Post by: kraflab on June 14, 2012, 06:25:43 AM
yes, use "row-1'

You should not be incrementing or decrementing your index variable, in the middle of indexing an array, without a very good reason.


I use increments and decrements in arrays for shorthand.

while (i<5) q[i++]=0; for example
Title: Re: Using -- and ++
Post by: Z on June 14, 2012, 08:18:44 AM
The problem is when you use a variable and you change its value in the same expression, like a[i++] = i; or a[i++] = i++;

Then you should not do it like this, because the C compiler is allowed to do things in any order it wants, so it can increment first and then access the incremented value, or use the value before increment (I am not sure whether it is undefined in the example above, but it is in similar examples, like (i++)*(i++)).

If your program generates a list of objects and you want to store them in an array, then a[numObjects++] = newObject; is OK.
Title: Re: Using -- and ++
Post by: Krice on June 14, 2012, 09:04:03 AM
Things like this happens when people try to "pack" code in smaller, more compact form. It may look cool, but it's an endless source of possible bugs.

Also it looks like you are using a raw array. Do not do that. With dungeon map you can easily use an array class with out of index check. Then you can access the map even with bad values without crashing the program.
Title: Re: Using -- and ++
Post by: Pueo on June 14, 2012, 05:08:23 PM
Things like this happens when people try to "pack" code in smaller, more compact form. It may look cool, but it's an endless source of possible bugs.

Also it looks like you are using a raw array. Do not do that. With dungeon map you can easily use an array class with out of index check. Then you can access the map even with bad values without crashing the program.
I'm not sure what "raw array" means, but about the part about array classes; is that applicable in plain C?
What I did was make a struct and make an array of said struct.

I'm thinking of transferring out of C, some things are a pain.
Title: Re: Using -- and ++
Post by: Kalantir on June 14, 2012, 05:32:18 PM
the C compiler is allowed to do things in any order it wants, so it can increment first and then access the incremented value, or use the value before increment
Actually, I'm pretty sure it depends on whether you do array[++i] or array[i++].  If you do ++i, it increments first and then accesses the variable at the new iterator position.  if you do i++ it accesses the variable at the current iterator position and then increments.

Quote from: pueo
I'm thinking of transferring out of C, some things are a pain.
I couldn't even imagine myself designing a game (especially an rpg) without using the OOP features that are provided in C++.  It helps with code organization/readibility and saves you from having to do a lot of extra work.
Title: Re: Using -- and ++
Post by: Z on June 14, 2012, 05:41:56 PM
Yes, there is such a rule (actually the definition of i++ and ++i), but it is applicable only for the case if you use either "i++" or ++i" only once, and don't use "i" anywhere else in the expression.

Otherwise, you cannot assume anything. Read here (http://c-faq.com/expr/index.html) for more information (several points in this FAQ are about this).
Title: Re: Using -- and ++
Post by: NON on June 14, 2012, 05:51:03 PM
Quote
I'm not sure what "raw array" means, but about the part about array classes; is that applicable in plain C?
Raw array means just a simple low level array of memory addresses, like:
MyStruct myStruct[MAP_X_CELLS][MAP_Y_CELLS]

What I think Krice is talking about is wrapping this so you don't deal directly with this low level stuff. I'm not really familiar with C. But at least in C++ you could have something like:
Code: [Select]
class Map
{
public:
   MapCell* getMapCellAt(const int X, const int Y) {

      // Here you can check for out of bounds indexes

      return &mapCells[X][Y];
   }
private:
   MapCell mapCells[MAP_X_CELLS][MAP_Y_CELLS];
};

I'm doing fine without it though.
Title: Re: Using -- and ++
Post by: Krice on June 14, 2012, 08:32:29 PM
I'm not sure what "raw array" means, but about the part about array classes; is that applicable in plain C?

Yes, you can write functions to access the array. "Raw" means you access the array directly in several locations, but the danger is in the index value which can be illegal for that array. By concentrating access on one function (or two, get and put) you can always check if the index is not inside the array. Usually there will be situations like that no matter how careful you think you are with the index value.
Title: Re: Using -- and ++
Post by: guest509 on June 15, 2012, 08:58:49 AM
  What Krice is advocating might not seem all that important early on in a project. He advocates for early adoption of coding practice that will keep your project manageable well into the future. If you are at a level to understand and implement what he's saying then you really should.

  If not then just keep plugging along. Be prepared for a rewrite later though, as your skills improve.
Title: Re: Using -- and ++
Post by: NON on June 15, 2012, 09:30:21 AM
My rewrite was in the other direction; Had a class for it, but now using raw array instead.
Title: Re: Using -- and ++
Post by: kraflab on June 15, 2012, 09:09:30 PM
If you are at a level to understand and implement what he's saying then you really should.

Eh, if you aren't careless it really doesn't matter.  I've never bothered with that and I've never had an issue.  If you are careless though then yes it's a good practice, but for me I would label it an unnecessary one.