Scope, memory usage and goto
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Scope, memory usage and goto

Hybrid View

  1. #1
    Join Date
    Feb 2012
    Location
    Strasbourg, France
    Posts
    116

    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 ?

  2. #2
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,860

    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.

  3. #3
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    15,002

    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...
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  4. #4
    Join Date
    Feb 2012
    Location
    Strasbourg, France
    Posts
    116

    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center