C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Newbie Q - For Loop Continues

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
lakepeir@yahoo.com
Guest





PostPosted: Mon Nov 28, 2005 8:19 am    Post subject: Newbie Q - For Loop Continues Reply with 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);

}

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





PostPosted: Mon Nov 28, 2005 3:55 pm    Post subject: Re: Newbie Q - For Loop Continues Reply with quote




[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





PostPosted: Mon Nov 28, 2005 3:57 pm    Post subject: Re: Newbie Q - For Loop Continues Reply with quote



[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





PostPosted: Tue Nov 29, 2005 10:17 am    Post subject: Re: Newbie Q - For Loop Continues Reply with 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.


[ 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





PostPosted: Tue Nov 29, 2005 10:21 am    Post subject: Re: Newbie Q - For Loop Continues Reply with quote

[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





PostPosted: Tue Nov 29, 2005 1:33 pm    Post subject: Re: Newbie Q - For Loop Continues Reply with quote


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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.