Undefined variable when initialized in switch statement?

The question itself begs an obvious answer. In any case, here's a snippet of my code...

    switch(cSet)...

    case 8:{ //Special Characters
        finalSet = special;
        char* charSet = new char[special.size() + 1];
        charSet[special.size()] = 0; //Append null terminator
        memcpy(charSet, special.c_str(), special.size());
        break;
    }

    case 9:{ //Alphnumeric and Special character
        finalSet = all;
        char* charSet = new char[all.size() + 1];
        charSet[all.size()] = 0; //Append null terminator
        memcpy(charSet, all.c_str(), all.size());
        break;
    }
    ...

Note that finalSet is of type std::string . I'm needing to save it as a character array. After this statement, I call charSet outside of the switch statement:

    for(int i = 0; charSet; i++)
         printf("%s", charSet[i]);

Now, it is obvious that switch statements are conditional, so a variable may not always be declared. So Visual Studio 2012 is throwing the error " charSet is undefined." The way I have my switch statement structured, though, charSet will always be defined, or the program will exit in the default case.

To remedy this issue, I attempted to declare charSet outside the scope of the switch statement. When I do this, however, for some reason the compiler throws a read access error.

I'm curious as to how I can fix this issue.

Any constructive input is appreciated.

Error code when declaring outside of switch statement:

`Unhandled exception at 0x0F6616B3 (msvcr110d.dll) in cuda_comb.exe: 0xC0000005: Access violation reading location 0x00000061.`

When you took it out did you try (before the switch):

char* charSet = 0;

and then in all the switch statements remove the char * , so

char* charSet = new char[all.size() + 1];

becomes:

charSet = new char[all.size() + 1];

then your code can even check (after the switch):

if (!charSet)
{
  // handle odd case
}

  • You have to declare charSet outside the switch . When the variable is declared inside the switch/case it will run out of scope when the switch ends, even if the case that declares the variable is executed.
  • Your for(int i = 0; charSet; i++) loops as long as charSet isn't 0 and increments i every time. Because you don't change charSet inside the for it will always not be 0 so that eventually charSet[i] is out of bounds and gives you the read access error. Change it so that it loops until i is greater than the length of the buffer, or charSet[i] == '0' (the terminating-null). For example:

    for(int i = 0; charSet[i]; i++) 
        printf("%c", charSet[i]);
    
  • Also change the %s in the printf to %c as you're printing a char , not a char * . Although, as you're printing a null-terminated string anyway, you can avoid the for loop altogether:

    printf("%s", charSet);
    

  • You're defining charSet on every case block, although you allocated memory on the heap for this array, outside these blocks, charSet reference is undefined as the builder is showing.

    Note that, by doing this, if you were not using charSet outside switch block, you will receive no error but you will be leaking memory.

    I mean:

    switch(cSet)...
    
    case 8:{ //Special Characters
        finalSet = special;
        char* charSet = new char[special.size() + 1]; // <-- charSet definition
        charSet[special.size()] = 0; //Append null terminator
        memcpy(charSet, special.c_str(), special.size());
        break;
    } // <-- charSet reference lost, Memory leak
    
    case 9:{ //Alphnumeric and Special character
        finalSet = all;
        char* charSet = new char[all.size() + 1]; // charSet definition
        charSet[all.size()] = 0; //Append null terminator
        memcpy(charSet, all.c_str(), all.size());
        break;
    } // <-- charSet reference lost, Memory leak
    
    } // End of switch
    
        for(int i = 0; charSet; i++)
         printf("%s", charSet[i]); // <-- Error charSet is not defined
    

    Correct solution for this is declaring charSet outside the switch statement so you won't lose the reference:

    char* charSet = NULL;
    switch(cSet)...
    
    case 8:{ //Special Characters
        finalSet = special;
        charSet = new char[special.size() + 1];
        charSet[special.size()] = 0; //Append null terminator
        memcpy(charSet, special.c_str(), special.size());
        break;
    }
    ...
    }
    
    if(charSet)
       printf("%s", charSet);
    

    Note that, as you're printing an array with "%s", you can directly use charSet .

    链接地址: http://www.djcxy.com/p/84442.html

    上一篇: switch语句中的变量定义

    下一篇: 在switch语句中初始化时未定义的变量?