Welcome Guest ( Log In | Register )

Outline · [ Standard ] · Linear+

 [HELP] C++ for loop

views
     
TSasimo
post Feb 10 2006, 11:09 AM, updated 20y ago

New Member
*
Junior Member
27 posts

Joined: Jan 2003


CODE
int i, cnt;
for (i=0; i < cnt; i++)
{
 return 0;
}


when code above been run.
Will the for loop been executed since the cnt is not initialized?

viking81
post Feb 10 2006, 11:19 AM

New Member
*
Junior Member
48 posts

Joined: Jan 2006


hmm... why ask? just go and try it out
TSasimo
post Feb 10 2006, 11:24 AM

New Member
*
Junior Member
27 posts

Joined: Jan 2003


QUOTE(viking81 @ Feb 10 2006, 11:19 AM)
hmm... why ask? just go and try it out
*
i dun have a compiler right now.
could u pls try it out for me?
zionx
post Feb 10 2006, 11:35 AM

New Member
*
Junior Member
7 posts

Joined: Feb 2006


u cant even compile the code bcos the cnt isnot initialized yet.


xenon
post Feb 10 2006, 01:00 PM

On my way
****
Senior Member
502 posts

Joined: Jan 2005
From: Selangor

Can be compiled. But result unpredictable.
A new local variable, before initialization, may be holding the value 0xCCCCCCCC. But depends on the code generated by compiler.
A global variable, before initialization, is most likely to be 0.
TSasimo
post Feb 10 2006, 02:48 PM

New Member
*
Junior Member
27 posts

Joined: Jan 2003


QUOTE(xenon @ Feb 10 2006, 01:00 PM)
Can be compiled. But result unpredictable.
A new local variable, before initialization, may be holding the value 0xCCCCCCCC. But depends on the code generated by compiler.
A global variable, before initialization, is most likely to be 0.
*
now only i know global variable and local variable has its difference in auto-initialization...

TSasimo
post Feb 10 2006, 04:04 PM

New Member
*
Junior Member
27 posts

Joined: Jan 2003


QUOTE(xenon @ Feb 10 2006, 01:00 PM)
Can be compiled. But result unpredictable.
A new local variable, before initialization, may be holding the value 0xCCCCCCCC. But depends on the code generated by compiler.
A global variable, before initialization, is most likely to be 0.
*
anyway, i think this is depends on the compiler we use, rite?
Loner
post Feb 10 2006, 04:15 PM

Getting Started
**
Junior Member
205 posts

Joined: Jan 2003
From: Kelana Jaya



it will compile and run, the loop execute only once and then it quit.
xenon
post Feb 10 2006, 04:29 PM

On my way
****
Senior Member
502 posts

Joined: Jan 2005
From: Selangor

When entering a function, there is code to adjust the stack pointer to reserve space for local variables. Uninitialized local variables have values that come from old values from the stack that already have been pop-ed. The values are unpredictable.

When using Microsoft development tools, uninitialized local variables are set to 0xCCCCCCCC.
lmcckl
post Feb 11 2006, 04:20 PM

Getting Started
**
Junior Member
50 posts

Joined: Dec 2005
From: KL


QUOTE(Loner @ Feb 10 2006, 04:15 PM)
it will compile and run, the loop execute only once and then it quit.
*
U try on complier? likely diffrent c compiler on diffrent paltform will show diffrent result...

interesting topic biggrin.gif
lohmk
post Feb 11 2006, 05:01 PM

Getting Started
**
Junior Member
234 posts

Joined: Jan 2003
From: Penang, Malaysia



your code is incomplete.
it all depends on where u put this code

lets say if its in the main(), it depends on the return type of your main() function.
of course, cnt is not initialized and the default value of int if not initialized is 0.
the loop will not run.

even if it runs, the function return 0. means that the loop will only loop once before exiting the main(), provided that i < cnt.

if your main() returns void, expect a compile error.


i'm still learning, correct me if i'm wrong.
silverhawk
post Feb 11 2006, 08:16 PM

Eyes on Target
Group Icon
Elite
4,956 posts

Joined: Jan 2003


The person was asking if that specific code would be able to run. You need not show the entire source if all you need to know is a small portion smile.gif

As some other has said, it depends on what value the variable is assigned when its declared. Its unpredictable...

a better question would be... why aren't you initialising your variables with a known value?
astalavista_baby
post Feb 12 2006, 01:41 AM

Enthusiast
*****
Senior Member
818 posts

Joined: Jan 2003
Whether the code will run or even compile very much depends on the compiler.

As said by someone, uninitialized variables will hold unknown values and hence even if the program runs, the result might be unpredictable. It is always a good idea to initialize the variables. smile.gif
Ken
post Feb 15 2006, 02:24 PM

Immigrants @ Jewish
*******
Senior Member
4,457 posts

Joined: Jul 2005
i thought compiler will auto initialize the value as 0 if use doesn't initialize it ...

means the code wont execute...
silverhawk
post Feb 15 2006, 02:58 PM

Eyes on Target
Group Icon
Elite
4,956 posts

Joined: Jan 2003


QUOTE(Ken @ Feb 15 2006, 02:24 PM)
i thought compiler will auto initialize the value as 0 if use doesn't initialize it ...

means the code wont execute...
*
That depends on the compiler, it often holds some random value or an address number.
You can try, its quite easy

CODE

int a;

cout << a;


Then see what you get.
Loner
post Feb 15 2006, 03:19 PM

Getting Started
**
Junior Member
205 posts

Joined: Jan 2003
From: Kelana Jaya



QUOTE(lmcckl @ Feb 11 2006, 04:20 PM)
U try on complier? likely diffrent c compiler on diffrent paltform will show diffrent result...

interesting topic biggrin.gif
*
yes I did tongue.gif using Dev-C++ compiler

 

Change to:
| Lo-Fi Version
0.0170sec    0.27    5 queries    GZIP Disabled
Time is now: 24th December 2025 - 01:16 AM