Thread: cross initialization of object

  1. #1
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446

    cross initialization of object

    Consider this:

    Code:
    int val = 0;
    switch(val) {
        case 1:
            int object = 0;
            break;
        case 2:
            break;
        default:
            break;
    }
    GCC 3.4.5 gives me a crosses initialization of 'int object' error. Which is fine and solvable with a pair of curly braces.

    But I'm curious. I cannot find on the standard any reference to this. It is an error indeed, if I try to use object on another case label. But I'm really not doing it above.

    Is this gcc specific or is indeed an error specified by the standard?
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    It's part of the C grammar. It's standard.
    http://www.lysator.liu.se/c/ANSI-C-g...html#statement

    The code within the brackets forms a compound statement. Declarations come before statements in C.
    Code:
    case 1:
    is a statement.

    ...
    Dumb forum won't let me post this without using code tags.
    Callou collei we'll code the way
    Of prime numbers and pings!

  3. #3
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Hey, we worked HARD for that script!

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Ok. That makes sense, I guess. (Am a slow grammar reader).

    Always expected that to be a run-time error (and only on the case of trying to access the undefined object). Was happily surprised to see a compile-time error. But it got me curious.

    Thanks.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Actually, that answer makes no sense at all. This is C++, which allows declarations anywhere.

    The C++ standard does say, however, that you may not jump past a declaration. See 6.7&#167;3.
    It is possible to transfer into a block, but not in a way that bypasses declarations with initialization. A program that jumps&#178; 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 and is declared without an initializer. [Example:
    Code:
    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]

    &#178; The transfer from the condition of a switch statement to a case label is considered a jump in this respect.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Aha! Thanks CornedBee. Couldn't find that reference.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    case 1:
    No tags necessary.
    It's only the curly braces &#123; and &#125; which trigger the script into thinking it might be code.
    Simple one-liners can be posted just fine.

    > Dumb forum won't let me post this without using code tags.
    Which is absolute bliss as far as I'm concerned as now 99% of code is posted properly rather than looking like trash.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    > It's only the curly braces &#123; and &#125;
    Yeah, you're a mod, you can bypass that, we know...

    So the script will force all posters to enclose curly braces in code tags...
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Direct X initialization
    By Know_Your_Role in forum C++ Programming
    Replies: 1
    Last Post: 07-08-2009, 04:05 PM
  2. Telling a shared_ptr not to delete object?
    By TriKri in forum C++ Programming
    Replies: 5
    Last Post: 08-16-2008, 04:26 AM
  3. Minimax, URGENT :(
    By Dark-MX0Z in forum C++ Programming
    Replies: 6
    Last Post: 11-14-2007, 06:29 AM
  4. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 04:33 PM
  5. Set Classes
    By Nicknameguy in forum C++ Programming
    Replies: 13
    Last Post: 10-31-2002, 02:56 PM