-
June 20th, 2012, 07:27 AM
#1
Scope, memory usage and goto
Code:
Start:
{
int i = 0;
i++;
goto Start;
}
My teacher told me never to use goto statements, trying to figure if there's another reason apart making the code less reable
Was wondering if this would cause an infinite memory usage because of redeclaring a variable each "loop" ? Or will the garbage collector empties the memory once we go out of scope ?
If the second is yes then what about that
Code:
{
Start:
int i = 0;
i++;
goto Start;
}
This one should use a lot of memory, no ?
-
June 20th, 2012, 11:19 AM
#2
Re: Scope, memory usage and goto
In both cases it would be an infinite loop, I do not think the memory usage would be much different but it would lock the program and use 100% cpu
Your teacher is correct in telling you not to use gotos while there may be some rare occasions where it would be the thing to do as a general rule of thumb they should not be used and will make your program hard to follow, hard to debug and show anyone who reads your code that you have not developed proper programming methods.
Always use [code][/code] tags when posting code.
-
June 20th, 2012, 01:24 PM
#3
Re: Scope, memory usage and goto
More like pulling the EMERGENCY BRAKE in extreme situations. Might work when you need it, but not good for the brakes...
-
June 22nd, 2012, 04:16 AM
#4
Re: Scope, memory usage and goto
Well i tried something else
Code:
Console.WriteLine("a");
goto test;
int i;
i = 23;
test:
i = 54;
Console.WriteLine(i.ToString());
Console.ReadKey(true);
I told myself this should raise an exception as i'm putting 54 in i while i is not declared/initalized.
And what happens is that my console actually prints 54 !
I decompiled the IL (with ILSpy) and found that :
Code:
Console.WriteLine("a");
Console.WriteLine(54.ToString());
Console.ReadKey(true);
How the compiler figured out that ? I didn't compiled in relase, i was in debug. Could someone explains me to what extent the compiler actually interprets the code ?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
On-Demand Webinars (sponsored)
|