access violation segmentation fault

can somebody explain me what's the cause of error
the line which stops the program is tagged below

bool checkString( string s)
{
int m = 0;
for ( ; m <=( s.length()-1); m++ )
if ( s[m+1] < s[m] )
break;

if ( m == (s.length()-1) )
return true;
else return false;

}

int main()
{
int t;
cin >> t;
string s;

while (t--)
{
cin >> s;
int k = 0;
int lenght = s.length();
char* suit[static_cast<int>(pow(2,(s.length()-2)))];
for ( int i = 1; i < s.length(); i++ )
for ( int j = 1; j != 0; )
{
if ( checkString( s.substr(i,j)) )
{
*********** /*this statement has the error*/ strcpy(suit[k], s.c_str());

k++;}


if ( j == s.length())
j = -1;
j++;

}


delete suit;}



}
char* suit[static_cast<int>(pow(2,(s.length()-2)))];

This is illegal. The size of an array must be a compile time constant. You might want to disable whatever compiler extension you have enabled that allows it.

Were it legal, you would only be allocating space for pointers. Those pointers would point to random places in memory, so when you do strcpy(suit[k], s.c_str()); you're writing to random memory that you don't own. Surprise. That causes problems.
You have never allocated memory to suit[k] array.
Fix:
1
2
suit[k] = new char[strlen(s.c_str()) + 1];
strcpy(suit[k], s.c_str());


delete suit
You need to delete each of elements of suit too:
1
2
3
4
for(int i; i <██; ++i) {
    delete suit[i];
}
delete suit;
Topic archived. No new replies allowed.