• Advertisement
Sign in to follow this  

while loop MADNESS

This topic is 1777 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement

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.

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

// 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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites


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

Share this post


Link to post
Share on other sites

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 );
}

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

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;
    // ...
}

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement