 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
lakepeir@yahoo.com Guest
|
Posted: Mon Nov 28, 2005 8:19 am Post subject: Newbie Q - For Loop Continues |
|
|
Hello,
I need help with my for loop. The loop does not end. It should end
when i is one less than uBound. What happens is that when i is 1 less
than uBound, the loop continues with a weird value 5432325 (I have no
idea how this value is generated, uBound is less than 10.) and then i
is equal to zero again. Here's the code:
for (int i=0; i < uBound; i++)
for (int j=0; j < uBound2; j++)
{
SafeArrayPutElement(psaArray, &lVal, &j);
}
Thanks!
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Martin Bonner Guest
|
Posted: Mon Nov 28, 2005 3:55 pm Post subject: Re: Newbie Q - For Loop Continues |
|
|
[email]lakepeir (AT) yahoo (DOT) com[/email] wrote:
| Quote: | Hello,
I need help with my for loop. The loop does not end.
It should end
when i is one less than uBound.
Not according to the posted code. The posted code ends when i is equal |
to uBound.
| Quote: | What happens is that when i is 1 less
than uBound, the loop continues with a weird value 5432325 (I have no
idea how this value is generated, uBound is less than 10.) and then i
is equal to zero again.
How do you know? The code doesn't show any output. |
Are you looking at values in the debugger? Be careful, debuggers can
get confused - particularly when run on optimized code.
| Quote: | Here's the code:
for (int i=0; i < uBound; i++)
for (int j=0; j < uBound2; j++)
{
SafeArrayPutElement(psaArray, &lVal, &j);
}
|
You aren't using i in the loop. Did you post the entire code, or did
you simplify it before posting? If the latter, I suspect you OVER
simplified.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ulrich Eckhardt Guest
|
Posted: Mon Nov 28, 2005 3:57 pm Post subject: Re: Newbie Q - For Loop Continues |
|
|
[email]lakepeir (AT) yahoo (DOT) com[/email] wrote:
| Quote: | I need help with my for loop. The loop does not end. It should end
when i is one less than uBound. What happens is that when i is 1 less
than uBound, the loop continues with a weird value 5432325 (I have no
idea how this value is generated, uBound is less than 10.) and then i
is equal to zero again. Here's the code:
for (int i=0; i < uBound; i++)
for (int j=0; j < uBound2; j++)
{
SafeArrayPutElement(psaArray, &lVal, &j);
}
|
Like Victor Bazarov already said in mpvl, the code you show here is not
responsible for the behaviour you describe. The only thing odd is the way
it is indented, but that could also have been caused by you newsclient.
Uli
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Dan McLeran Guest
|
Posted: Tue Nov 29, 2005 10:17 am Post subject: Re: Newbie Q - For Loop Continues |
|
|
Are you sure that SafeArrayPutElement isn't mucking with j? Why pass
the address of j? Seems like asking for trouble. SafeArrayPutElement
should take an int parameter as a const int instead of int* or even
const int*. At least that way, your loop variable cannot be corrupted.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Branimir Maksimovic Guest
|
Posted: Tue Nov 29, 2005 10:21 am Post subject: Re: Newbie Q - For Loop Continues |
|
|
[email]lakepeir (AT) yahoo (DOT) com[/email] wrote:
| Quote: | Hello,
I need help with my for loop. The loop does not end. It should end
when i is one less than uBound. What happens is that when i is 1 less
than uBound, the loop continues with a weird value 5432325 (I have no
idea how this value is generated, uBound is less than 10.) and then i
is equal to zero again. Here's the code:
for (int i=0; i < uBound; i++)
for (int j=0; j < uBound2; j++)
{
SafeArrayPutElement(psaArray, &lVal, &j);
}
|
You should check if SafeArrayPutElement causes undefined behavior
(overwrites the stack).
Greetings, Bane.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Martin Bonner Guest
|
Posted: Tue Nov 29, 2005 1:33 pm Post subject: Re: Newbie Q - For Loop Continues |
|
|
Dan McLeran wrote:
| Quote: | Are you sure that SafeArrayPutElement isn't mucking with j? Why pass
the address of j? Seems like asking for trouble. SafeArrayPutElement
should take an int parameter as a const int instead of int* or even
const int*. At least that way, your loop variable cannot be corrupted.
|
He doesn't get to choose the signature of SafeArrayPutElement. It is a
Microsoft function (or possibly a clone of that). The first argument
is a "Safe Array" descriptor. The second is an array of indices, and
the third is a pointer to the data to be stored in the safe array. The
fact that he isn't updating the indices makes me think he has
oversimplified the code for posting.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|