Undefined variable when initialized in switch statement?

0

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.`
c++
switch-statement
asked on Stack Overflow Nov 25, 2013 by Mlagma • edited Nov 25, 2013 by Mlagma

4 Answers

5
  1. 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.
  2. 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]);
    
  3. 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);
    
answered on Stack Overflow Nov 25, 2013 by Kninnug • edited Nov 25, 2013 by Kninnug
2

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
}
answered on Stack Overflow Nov 25, 2013 by Glenn Teitelbaum
2

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.

answered on Stack Overflow Nov 25, 2013 by jcm • edited Nov 25, 2013 by jcm
0

From what I understand your code looks like this:

 switch(cSet){    
        case 8:{ //Special Characters           
           char* charSet  = new char[special.size() + 1];
           //code
           break;
          }
         case 9:{ //Special Characters           
           char* charSet = new char[all.size() + 1];
           //code
           break;
           }
        //other cases
     }

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

In this case even though you define charset for each case, you restrict the scope of charset to each case where it's defined - which you already figured out and moved the pointer declaration outside the switch - like:

char* charSet;
switch(cSet){    
       //code
}

The error you are getting basically says you are reading from an invalid memory location. Which can happen if you don't assign the pointer to a dynamically allocated memory location. Even though you seem to allocate memory for charSet for all cases of the switch statement, don't forget you have a default case as well.

Hope it helps.

answered on Stack Overflow Nov 25, 2013 by Pandrei

User contributions licensed under CC BY-SA 3.0