Jump to content

  • Log In with Google      Sign In   
  • Create Account

while loop MADNESS


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
16 replies to this topic

#1 greenzone   Members   -  Reputation: 672

Like
1Likes
Like

Posted 11 March 2013 - 01:55 PM

Ok so i am working on this program using PhysX and one of the samples that comes with the library has a while loop that runs in the rendercall back function. like so

 

 

    int nbActors = gScene->getNbActors();
    NxActor** actors = gScene->getActors();
    while(nbActors--){

 

NxActor* actor = *actors++;

///render all the stuff with physics :)

 

 

}

 

What i dont understand is that while loop. whats going on there exactly. from what i would guess its like incrementing through the number of actors and running the loop for each one but im not sure. could some one explaine this use of a while loop and or perhaps the use of the decrementer as something being passed like that.

 

Thanks


J-GREEN

Greenpanoply

Sponsor:

#2 GeneralQuery   Crossbones+   -  Reputation: 1263

Like
3Likes
Like

Posted 11 March 2013 - 02:16 PM

The NxActor* actor = *actors++ line is getting a pointer to the next object in the array pointed to by actors using pointer arithmetic. The while loop is looping nbActors number of times which I'm assuming in turn specifies the number of elements in the array of NxActors pointed to by actors.



#3 greenzone   Members   -  Reputation: 672

Like
0Likes
Like

Posted 11 March 2013 - 02:23 PM

so like is this loop is going to loop until nbActors-- reaches 0 before it breaks out?

 

also

 

If i wanted to specify a specific actor in that array " *actors++ " and start looping upwards from there how would that be done? for example if i wanted to start at index 5 and then ++ from there how would that be done?


J-GREEN

Greenpanoply

#4 Bregma   Crossbones+   -  Reputation: 5133

Like
9Likes
Like

Posted 11 March 2013 - 02:24 PM

Ok so i am working on this program using PhysX and one of the samples that comes with the library has a while loop that runs in the rendercall back function. like so

 

 

    int nbActors = gScene->getNbActors();
    NxActor** actors = gScene->getActors();
    while(nbActors--){

 

NxActor* actor = *actors++;

///render all the stuff with physics smile.png

 

 

}

 

What i dont understand is that while loop. whats going on there exactly. from what i would guess its like incrementing through the number of actors and running the loop for each one but im not sure. could some one explaine this use of a while loop and or perhaps the use of the decrementer as something being passed like that.

That while-loop is an obfuscated way of writing this for-loop.

int nbActors = gScene->getNbActors();
for (int i = 0; i < nbActors; ++i)
{
    NxActor* actor = actors[i];
    // ...
}

the world is full of Klever Koders.


Stephen M. Webb
Professional Free Software Developer

#5 GeneralQuery   Crossbones+   -  Reputation: 1263

Like
4Likes
Like

Posted 11 March 2013 - 02:34 PM

so like is this loop is going to loop until nbActors-- reaches 0 before it breaks out?

 

also

 

If i wanted to specify a specific actor in that array " *actors++ " and start looping upwards from there how would that be done? for example if i wanted to start at index 5 and then ++ from there how would that be done?

See Bregma's post above for further insight to your OP. Regarding your further questions, consider this code snippet:

 

 

int array[10];
int* p = array; // p points to first element of the array, i.e. array[0]
 
p += 4; // p points to 5th element of array, i.e.  array[4]
 
cout<<*p; // dereference p to access the data at array[4] and print the result
 
cout<<*(p-1); // dereference p to access the data at array[3] and print the result

int* q = p++; // as p is still pointing to array[4], q points to array[5]

// and so on...
 

Edited by GeneralQuery, 11 March 2013 - 02:37 PM.


#6 swiftcoder   Senior Moderators   -  Reputation: 10000

Like
5Likes
Like

Posted 11 March 2013 - 02:53 PM

int nbActors = gScene->getNbActors();

NxActor** actors = gScene->getActors();


while(nbActors--){ // Line A

     NxActor* actor = *actors++; // Line B


    ///render all the stuff with physics

}

 

Line (A) exploits that non-zero integers evaluate to true, while zero evaluates to false. The while loop will count down from the number of actors (the 'x--' decrement operator returns the current value of x, and afterwards reduces the value of x by one), until the number is zero.

 

Line (B) exploits the fact that items in an array are allocated contiguously (i.e. one after another), to move a pointer through the array (the 'x++' increment operator is the opposite of decrement, it returns x and then increases the value of x by one).


Tristam MacDonald - Software Engineer @Amazon - [swiftcoding]


#7 greenzone   Members   -  Reputation: 672

Like
0Likes
Like

Posted 11 March 2013 - 03:07 PM

So swiftcoder, thank you very much well written and that helped me indeed.

 

but in Line B if i wanted to start at index 4 of actors, then increment up from there, how would i write that?

I know its a simple answer I am just not understanding or thinking about it correctly.

while(nbActors--){ // Line A

     NxActor* actor = *actors++; // Line B


    ///render all the stuff with physics

}


Edited by greenzone, 11 March 2013 - 03:35 PM.

J-GREEN

Greenpanoply

#8 Paradigm Shifter   Crossbones+   -  Reputation: 5375

Like
1Likes
Like

Posted 11 March 2013 - 03:39 PM


// Like this

actors += 4;   // start from element 4 instead of 0
nbActors -= 4; // still got the same number though so reduce initial loop count

while(nbActors--){ // Line A

     NxActor* actor = *actors++; // Line B


    ///render all the stuff with physics

}

EDIT: You need to check that nbActors (after subtracting 4) is >= 1 before you enter the while loop bit though otherwise bad things will happen.


Edited by Paradigm Shifter, 11 March 2013 - 03:42 PM.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

#9 greenzone   Members   -  Reputation: 672

Like
0Likes
Like

Posted 11 March 2013 - 03:51 PM

oh I am sorry, but i should have included that I am using the first 3 values in that array within that loop. so imagine if actors[0] - actors[4] are being used and passed to some other function or variable with in the loop. Then after I am wanting to take the rest of the actors in the array, actors[4] and upward, and increment upwards as in Line B.

 

 

How would I then increment upwards knowing i dont want to disturb actors 0-4 in the array?

 

for example:

while(nbActors--){ // Line A

    NxActor* actor0 = *actors[0]; 
    NxActor* actor1 = *actors[1]; 
    NxActor* actor2 = *actors[2]; 
    NxActor* actor3 = *actors[3]; 


     NxActor* actor = *actors++; // Line B     I want this line to up date every loop 
                                            // with new actors number counting upward from 4


    ///render all the stuff with physics

}

Edited by greenzone, 11 March 2013 - 03:54 PM.

J-GREEN

Greenpanoply

#10 Paradigm Shifter   Crossbones+   -  Reputation: 5375

Like
1Likes
Like

Posted 11 March 2013 - 03:57 PM



How would I then increment upwards knowing i dont want to disturb actors 0-4 in the array?

 

Like this:


NxActor* actor0 = actors[0];
NxActor* actor1 = actors[1];
NxActor* actor2 = actors[2];
NxActor* actor3 = actors[3];

actors += 4; // start from element 4 instead of 0
nbActors -= 4; // still got the same number though so reduce initial loop count

if(nbActors > 0)
{
    while(nbActors--){ // Line A
        NxActor* actor = *actors++; // Line B     

    ///render all the stuff with physics
    }
}

Alternatively:

 

 

NxActor* actor0 = *actors++;
NxActor* actor1 = *actors++;
NxActor* actor2 = *actors++;
NxActor* actor3 = *actors++;

nbActors -= 4; // still got the same number though so reduce initial loop count

if(nbActors > 0)
{
    while(nbActors--){ // Line A
        NxActor* actor = *actors++; // Line B

    ///render all the stuff with physics
    }
}
 

Edited by Paradigm Shifter, 11 March 2013 - 04:08 PM.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

#11 swiftcoder   Senior Moderators   -  Reputation: 10000

Like
3Likes
Like

Posted 11 March 2013 - 04:00 PM

So, to be honest, I'd never recommend writing a loop like this. It's a style favoured by a handful of old-school C programmers, and it is hard to read and maintain even for experienced devs.

 

Prefer something like this:

do_something( actors[0], actors[1], actors[2], actors[3] );
 
for (int i = 4; i < numActors; ++i) {
    do_something_else( actors[i] );
}

Tristam MacDonald - Software Engineer @Amazon - [swiftcoding]


#12 Paradigm Shifter   Crossbones+   -  Reputation: 5375

Like
3Likes
Like

Posted 11 March 2013 - 04:02 PM

Yeah, I'd recommend using another array to hold the special first 4 elements as well but since they asked how to do it I told them ;)


"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

#13 Khatharr   Crossbones+   -  Reputation: 3003

Like
1Likes
Like

Posted 11 March 2013 - 07:56 PM

int nbActors = gScene->getNbActors();
NxActor** actors = gScene->getActors();
while(nbActors--) {
  NxActor* actor = *actors++;
  ///render all the stuff with physics :)
}

 

...

 

SOMEONE GET THIS MAN A FOR-LOOP!


Edited by Khatharr, 11 March 2013 - 07:57 PM.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

#14 TheChubu   Crossbones+   -  Reputation: 4364

Like
1Likes
Like

Posted 11 March 2013 - 09:21 PM

Ok so i am working on this program using PhysX and one of the samples that comes with the library has a while loop that runs in the rendercall back function. like so

 

 

    int nbActors = gScene->getNbActors();
    NxActor** actors = gScene->getActors();
    while(nbActors--){

 

NxActor* actor = *actors++;

///render all the stuff with physics smile.png

 

 

}

 

What i dont understand is that while loop. whats going on there exactly. from what i would guess its like incrementing through the number of actors and running the loop for each one but im not sure. could some one explaine this use of a while loop and or perhaps the use of the decrementer as something being passed like that.

That while-loop is an obfuscated way of writing this for-loop.

 

int nbActors = gScene->getNbActors();
for (int i = 0; i < nbActors; ++i)
{
    NxActor* actor = actors[i];
    // ...
}

the world is full of Klever Koders.

This. Looks like a way to purposely obfuscate a loop more than anything else. Its almost "Coding Horrors" subforum worthy.


Edited by TheChubu, 11 March 2013 - 09:23 PM.

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

 

My journals: dustArtemis ECS framework and Making a Terrain Generator


#15 Paradigm Shifter   Crossbones+   -  Reputation: 5375

Like
1Likes
Like

Posted 11 March 2013 - 09:39 PM

Hmm, not really. It's just counting backwards, which was a win on the PS1 cos you had a hard wired register that always read zero so comparing to zero was faster than checking the loop condition. If the optimiser doesn't do it, you should compare against zero anyway since you don't need to do a compare you just check the zero flag... The optimiser probably does it though...


"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

#16 Khatharr   Crossbones+   -  Reputation: 3003

Like
6Likes
Like

Posted 12 March 2013 - 04:40 AM

Hmm, not really. It's just counting backwards, which was a win on the PS1 cos you had a hard wired register that always read zero so comparing to zero was faster than checking the loop condition. If the optimiser doesn't do it, you should compare against zero anyway since you don't need to do a compare you just check the zero flag... The optimiser probably does it though...

 

This is an example of bad optimization:

  • It's a level 3 'wtf' to anyone who encounters it.
  • The number of cycles saved by the comparison optimization is beyond trivial.
  • It's error prone. (I'd be delighted if gScene->getNbActors() returns -1 on error.)
  • Based on what it's doing I doubt that it's within a tight loop -> it doesn't need to be optimized.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

#17 swiftcoder   Senior Moderators   -  Reputation: 10000

Like
2Likes
Like

Posted 12 March 2013 - 08:58 AM

Hmm, not really. It's just counting backwards, which was a win on the PS1 cos you had a hard wired register that always read zero so comparing to zero was faster than checking the loop condition. If the optimiser doesn't do it, you should compare against zero anyway since you don't need to do a compare you just check the zero flag... The optimiser probably does it though...

And if this snippet was from a tutorial on optimising loops for the PS1, fair enough.

 

Since it's meant to be a tutorial on writing physics callbacks, not so much...


Tristam MacDonald - Software Engineer @Amazon - [swiftcoding]





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS