quick goto question

This is a discussion on quick goto question within the C++ Programming forums, part of the General Programming Boards category; I just have a quick goto question. if you have some code like Code: start: int a; cin >> a; ...

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    40

    quick goto question

    I just have a quick goto question.

    if you have some code like

    Code:
    start:
    
    int a;
    cin >> a;
    cout << a;
    
    goto start;
    how does that handle the variable declaration?

    Does it skip over it after the first time becuase a is already declared?
    Or does it create it over and over agian?

    I know that goto statments are a bad idea, i was just curious about this.

    thanks

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    I can't believe I am seeing GOTO in an object oriented language.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  3. #3
    Registered User cfrost's Avatar
    Join Date
    Apr 2004
    Posts
    119
    well it will not create a variable again instread it will use previously created with a prev value
    Software is like sex it is good when it is free

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    The code won't be interpreted exactly as the source says. The variable will be pushed onto the stack before entering the actual function. If the variable is a class or you assign it a value its constructor/assignment will be executed every time though (the only exception being static variables).
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    This doesn't compile:
    Code:
    int main(){
    	goto aaa;
    	int* something=new int();
    
    aaa:
    	*something=0;
    	return 0;
    }
    And the compiler errors (MS VC++6):
    Code:
     error C2362: initialization of 'something' is skipped by 'goto aaa'
            see declaration of 'something'

  6. #6
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    Hello,

    C provides the infinitely-abusable goto statement, and labels branch to. Formally, the goto statement is never necessary, and in practice it is almost always easy to write code without it.

    Furthermore, in the goto statement, the identifier must be a label located in the current function. Control transfers to the labeled statement.

    Quote Originally Posted by xErath
    error C2362: initialization of 'something' is skipped by 'goto aaa' see declaration of 'something'
    These errors are generated because the scope of the variable, when it is defined and initialized, is in the same scope as the case label or the goto label. Thus, there is a chance that the initialization will not occur. There are ways to ensure that the initialization is performed:

    • Enclose the for loop in "{}" (curly braces). This will cause the for loop to be in a different scope than the case and goto labels.
    • Define the loop variable right before entering the loop. This works only for simple types, not for user-defined types.
    • Define the variable without initializing it. Any assignments made to that variable after its definition are considered assignments, not initializations.


    This problem may occur with other compilers, though directly to MSVC in this particular case.


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    CPP Standard Section 6.7 Paragraph 3
    It is possible to transfer into a block, but not in a way that bypasses declarations with initialization. A program
    that jumps77) from a point where a local variable with automatic storage duration is not in scope to a
    point where it is in scope is ill-formed unless the variable has POD type (3.9) and is declared without an
    initializer (8.5).
    Code:
    [Example:
    void f()
    {
    // ...
    goto lx; // ill-formed: jump into scope of a
    // ...
    ly:
    X a = 1;
    // ...
    lx:
    goto ly; // OK, jump implies destructor
    // call for a followed by construction
    // again immediately following label ly
    }
    —end example]
    Closest thing I could find ATM to what the OP wanted:
    CPP Standard Section 15 Paragraph 2
    A goto, break, return, or continue statement can be used to transfer control out of
    a try block or handler. When this happens, each variable declared in the try block will be destroyed in the
    context that directly contains its declaration.
    Code:
    lab: try {
    T1 t1;
    try {
    T2 t2;
    if (condition)
    goto lab;
    } catch(...) { /* handler 2 */ }
    } catch(...) { /* handler 1 */ }
    Of course thats in different blocks so it might be different if its in the same block

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Formally, the goto statement is never necessary, and in practice it is almost always easy to write code without it.
    Never say never
    The discussion has been discussed and within some very tight requirements there has been a need for a goto.

  9. #9
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Idea: Make the compiler generate warnings on goto. That way, by sheer power of annoyance, it will teach people to find ways around it wherever possible, assuming they don't figure out a way to disable the warning, but it's still there for you as long as you're absolutely sure you want to use it.

    Warning, line 26: Use of goto is highly discouraged, and may cause you to die unless used under tight regulation. Please consider revising your statement, for the sake of all parties concerned.
    Last edited by Hunter2; 11-17-2004 at 11:20 AM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  10. #10
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    I don't know Hunter that sounds a lot like this problem

  11. #11
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Well, if they're going to take it that way, then maybe it's for the best that they spend their time on other things
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  12. #12
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    goto statements only have good uses when control flow passes down from the function. In terms of style and understanding, code such as
    Code:
    void f(int i) 
    {
          if (i == 0)
               goto exit;
    
          std::cout << "Hello" << std::endl;
    
    exit:
    }
    is the same as
    Code:
    void f(int i) 
    {
          if (i == 0)
               return;
    
          std::cout << "Hello" << std::endl;
    }
    The issue concerning object constructor initialization also occurs with switch statements. For example, you cannot write
    Code:
    switch(1) {
    case 1: 
            int foo;
            break;
    case 2:
            break;
    }
    (Each case does not define a new lexical scope block. )

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. quick question (adding characters)
    By Cactus in forum C Programming
    Replies: 2
    Last Post: 09-24-2005, 03:54 PM
  2. very quick question.
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 07-24-2002, 03:48 AM
  3. quick question
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 04:44 AM
  4. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21