CODE
int i, cnt;
for (i=0; i < cnt; i++)
{
return 0;
}
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?
[HELP] C++ for loop
|
|
Feb 10 2006, 11:09 AM, updated 20y ago
Show posts by this member only | Post
#1
|
![]()
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? |
|
|
|
|
|
Feb 10 2006, 11:19 AM
Show posts by this member only | Post
#2
|
![]()
Junior Member
48 posts Joined: Jan 2006 |
hmm... why ask? just go and try it out
|
|
|
Feb 10 2006, 11:24 AM
Show posts by this member only | Post
#3
|
![]()
Junior Member
27 posts Joined: Jan 2003 |
|
|
|
Feb 10 2006, 11:35 AM
Show posts by this member only | Post
#4
|
![]()
Junior Member
7 posts Joined: Feb 2006 |
u cant even compile the code bcos the cnt isnot initialized yet.
|
|
|
Feb 10 2006, 01:00 PM
Show posts by this member only | Post
#5
|
![]() ![]() ![]() ![]()
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. |
|
|
Feb 10 2006, 02:48 PM
Show posts by this member only | Post
#6
|
![]()
Junior Member
27 posts Joined: Jan 2003 |
QUOTE(xenon @ Feb 10 2006, 01:00 PM) Can be compiled. But result unpredictable. now only i know global variable and local variable has its difference in auto-initialization...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. |
|
|
|
|
|
Feb 10 2006, 04:04 PM
Show posts by this member only | Post
#7
|
![]()
Junior Member
27 posts Joined: Jan 2003 |
QUOTE(xenon @ Feb 10 2006, 01:00 PM) Can be compiled. But result unpredictable. anyway, i think this is depends on the compiler we use, rite?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. |
|
|
Feb 10 2006, 04:15 PM
Show posts by this member only | Post
#8
|
![]() ![]()
Junior Member
205 posts Joined: Jan 2003 From: Kelana Jaya |
it will compile and run, the loop execute only once and then it quit.
|
|
|
Feb 10 2006, 04:29 PM
Show posts by this member only | Post
#9
|
![]() ![]() ![]() ![]()
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. |
|
|
Feb 11 2006, 04:20 PM
|
![]() ![]()
Junior Member
50 posts Joined: Dec 2005 From: KL |
|
|
|
Feb 11 2006, 05:01 PM
|
![]() ![]()
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. |
|
|
Feb 11 2006, 08:16 PM
|
|
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
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? |
|
|
Feb 12 2006, 01:41 AM
|
![]() ![]() ![]() ![]() ![]()
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. |
|
|
|
|
|
Feb 15 2006, 02:24 PM
|
![]() ![]() ![]() ![]() ![]() ![]() ![]()
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... |
|
|
Feb 15 2006, 02:58 PM
|
|
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 ... That depends on the compiler, it often holds some random value or an address number.means the code wont execute... You can try, its quite easy CODE int a; cout << a; Then see what you get. |
|
|
Feb 15 2006, 03:19 PM
|
![]() ![]()
Junior Member
205 posts Joined: Jan 2003 From: Kelana Jaya |
|
| Change to: | 0.0149sec
0.36
5 queries
GZIP Disabled
Time is now: 24th December 2025 - 05:47 AM |