Solved

FOR loop variable

Posted on 2000-04-28
10
192 Views
Last Modified: 2010-04-15
What happens with X in the following code ? Where is it stored ? How many times is it stored ?
(Basically what would the assembly look like ?)

for( int i=0;i<i+1; i++ )
{
  unsigned char X;<- What happens here ?
  do_something( &X );
}

Is there any difference if it would have
been
unsigned char X=1; ??

0
Comment
Question by:CrypToniC
  • 5
  • 3
  • 2
10 Comments
 
LVL 10

Accepted Solution

by:
rbr earned 50 total points
ID: 2758804
Stored locally on the stack. Livetime between the 2 { }. So the variable starts living after the { and is removed by the }

The only differnces between
unsigned char X;
and
unsigned char X=1;

is that in the second case the X becomes 1 at the start of the loop.

btw.

for( int i=0;i<i+1; i++ )

while loop till an overflow.
0
 
LVL 1

Author Comment

by:CrypToniC
ID: 2758813
Locally on the stack only
initialized (and copied) once ?

BTW yez it does overflow but it was
just a silly example :)
0
 
LVL 10

Expert Comment

by:rbr
ID: 2758820
yes initalized onced. What to you mean with copied?
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 2

Expert Comment

by:anand_007
ID: 2758833
No! Your X is going to get initialized every time! So, once ur loop i executed, x is reinitialized during the next runof ur loop! By the way, it's dangerous to use ur comparison statement x<x+1 -- it'll always be so!! Stack dump indeed -- try using UNIX u'll be CORE-Dumped!  
0
 
LVL 10

Expert Comment

by:rbr
ID: 2758837
Pls read my answer carefully. I said x becomes 1 at the start at loop I mean it is intialized once for a loop.
0
 
LVL 1

Author Comment

by:CrypToniC
ID: 2758866
People forget about the syntax for the
darn FOR - loop it is not interesting

rbr - one last thing
Would you initialize X inside the FOR
or outside, consider the case where
x: unsigned char X . No initialization
is done

BTW if i is a 16-bit number
wouldn't for( i;i<i+1;i++)
be equal to for( ;; ), since 32767<(32767+1 == -32768) Why would it neccessarily mean a stack dump ?
0
 
LVL 10

Expert Comment

by:rbr
ID: 2758904
It depends on what you want to do.

e.g.

int main (int argn,char *argv[])
{

    int i;  
    printf ("Start");
 
   for (i=0;i<5;i++) {
       int j=1;
       j*=j;

       printf ("%d",j);
   }
   return 0;
}

and

int main (int argn,char *argv[])
{

   int i;  
   int j=1;
   printf ("Start");
 
   for (i=0;i<5;i++) {
       j*=j;

       printf ("%d",j);
   }
   return 0;
}

will lead to the same result. The differnces is that in the first example j is not accessable from outside the loop. (you could have a differnet j outside to loop)

32767<(32767+1 == -32768) is false so the loop ends. But many OSs produces a fault when a overflow occurs.
0
 
LVL 1

Author Comment

by:CrypToniC
ID: 2758917
So there is no performance difference ?
0
 
LVL 10

Expert Comment

by:rbr
ID: 2758922
Couldn't be answered for general. You don't know what your compiler does during optimization.
0
 
LVL 2

Expert Comment

by:anand_007
ID: 2761521
Right, now that we know about command line arguments, what's theproblem again??
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question