Author Topic: Using -- and ++  (Read 14557 times)

Pueo

  • Rogueliker
  • ***
  • Posts: 263
  • Karma: +0/-0
    • View Profile
    • Email
Using -- and ++
« 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.
« Last Edit: June 14, 2012, 04:33:03 AM by Pueo »
{O.o}
 |)__)
   ” ”   o RLY?

Hi

  • 7DRL Reviewer
  • Rogueliker
  • *
  • Posts: 154
  • Karma: +0/-0
    • View Profile
    • Email
Re: Using -- and ++
« Reply #1 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.
« Last Edit: June 14, 2012, 05:39:41 AM by Hi »

Pueo

  • Rogueliker
  • ***
  • Posts: 263
  • Karma: +0/-0
    • View Profile
    • Email
Re: Using -- and ++
« Reply #2 on: June 14, 2012, 06:13:04 AM »
yes, use "row-1'
Alright, thanks.
{O.o}
 |)__)
   ” ”   o RLY?

kraflab

  • Rogueliker
  • ***
  • Posts: 454
  • Karma: +0/-0
    • View Profile
    • kraflab.com
Re: Using -- and ++
« Reply #3 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

Z

  • Rogueliker
  • ***
  • Posts: 905
  • Karma: +0/-0
    • View Profile
    • Z's Roguelike Stuff
Re: Using -- and ++
« Reply #4 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.

Krice

  • (Banned)
  • Rogueliker
  • ***
  • Posts: 2316
  • Karma: +0/-2
    • View Profile
    • Email
Re: Using -- and ++
« Reply #5 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.
« Last Edit: June 14, 2012, 09:08:46 AM by Krice »

Pueo

  • Rogueliker
  • ***
  • Posts: 263
  • Karma: +0/-0
    • View Profile
    • Email
Re: Using -- and ++
« Reply #6 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.
{O.o}
 |)__)
   ” ”   o RLY?

Kalantir

  • Newcomer
  • Posts: 31
  • Karma: +0/-0
    • View Profile
Re: Using -- and ++
« Reply #7 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.
« Last Edit: June 14, 2012, 05:36:16 PM by Kalantir »

Z

  • Rogueliker
  • ***
  • Posts: 905
  • Karma: +0/-0
    • View Profile
    • Z's Roguelike Stuff
Re: Using -- and ++
« Reply #8 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 for more information (several points in this FAQ are about this).

NON

  • Rogueliker
  • ***
  • Posts: 349
  • Karma: +0/-0
    • View Profile
    • Infra Arcana
    • Email
Re: Using -- and ++
« Reply #9 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.
« Last Edit: June 14, 2012, 05:59:34 PM by NON »
Happy is the tomb where no wizard hath lain and happy the town at night whose wizards are all ashes.

Krice

  • (Banned)
  • Rogueliker
  • ***
  • Posts: 2316
  • Karma: +0/-2
    • View Profile
    • Email
Re: Using -- and ++
« Reply #10 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.
« Last Edit: June 14, 2012, 08:34:53 PM by Krice »

guest509

  • Guest
Re: Using -- and ++
« Reply #11 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.

NON

  • Rogueliker
  • ***
  • Posts: 349
  • Karma: +0/-0
    • View Profile
    • Infra Arcana
    • Email
Re: Using -- and ++
« Reply #12 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.
Happy is the tomb where no wizard hath lain and happy the town at night whose wizards are all ashes.

kraflab

  • Rogueliker
  • ***
  • Posts: 454
  • Karma: +0/-0
    • View Profile
    • kraflab.com
Re: Using -- and ++
« Reply #13 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.