0

I got the error: "Unhandled exception at 0x00d23737 in Test.exe: 0xC0000005: Access violation reading location 0x8a8c0344" This happens when i execute the following code:

int main(int argc, char* argv[])
{
string My_String_Array[30720];
Initialize_My_String_Array (My_String_Array); //i really doubt that there is something wrong in the definition of this function
for (int i=0;i<=30719;i++)
    {
    cout<<My_String_Array[i]<<endl;
    }
system("pause");
return 0;
}

The code to Initialize_My_String_Array() is as follows:

void Initialize_My_String_Array (My_String_Array[30720])
{   
    string Initialization_Values[]={"A","B","C","D","E"};
    int Index=0;
    int i=0;
    int j=0;
    while (i<=30719)
        {   

            My_String_Array[i]=Initialization_Values[Index];
            i++;
            j++;
            if (j=6144)
                {
                j=0;
                Index++;
                }
        }

Any thoughts would be appreciated.

1 Answers1

6

There are a number of problems in your code, particularly in Initialize_My_String_Array.

void Initialize_My_String_Array (My_String_Array[30720])
{   
    string Initialization_Values[]={"A","B","C","D","E"};
    int Index=0;
    int i=0;
    int j=0;
    while (i<=30719){
        My_String_Array[i]=Initialization_Values[Index];
        i++;
        j++;
        if (j=6144){
            j=0;
            Index++;
        }
    }
}
  1. Your method signature is incorrect. You've declared a variable My_String_Array[30720], but you haven't given it a type. From context, I think the type is supposed to be std::string.
  2. In the if statement, you've written if(j = 6144). = is not the equality operator, it is the assignment operator. == is the equality operator. Your code is assigning 6144 to j, then promoting the (non-zero) value to a boolean, which makes the if statement always execute, which means Index is always incremented. Within 5 iterations of the loop, Index is incremented beyond the size of Initialization_Values, which causes Undefined Behavior, and in your case, causing access violations.

Based on the context, I think the corrected code looks like this:

//Type is now correctly defined.
void Initialize_My_String_Array (string My_String_Array[30720])
{   
    string Initialization_Values[]={"A","B","C","D","E"};
    int Index=0;
    int i=0;
    int j=0;
    while (i<=30719){
        My_String_Array[i]=Initialization_Values[Index];
        i++;
        j++;
        if (j == 6144){
            j=0;
            Index++;
        }
    }
}

Beyond this, you need to spend some time reevaluating how you've written this code. C-style arrays are generally considered to be bad practice in C++, and your code is a perfect case-study as to why. C++ std::array objects are far superior, and should be preferred for any code like this. On top of that, you've indulged in some spaghetti code involving indexes and array bounds. There are much better ways to handle it. I've written what I consider to be a much better version of this code:

void Initialize_My_String_Array (std::array<std::string, 30720> & My_String_Array)
{   
    const std::array<std::string, 5> Initialization_Values{"A","B","C","D","E"};
    const size_factor = (My_String_Array.size() / Initialization_Values.size());
    for(size_t index = 0; index < My_String_Array.size() && (index / size_factor) < Initialization_Values.size(); index++) {
        My_String_Array[index] = Initialization_Values[index / size_factor];
    }
}

int main(int argc, char* argv[]) {
    std::array<std::string, 30720> My_String_Array;
    Initialize_My_String_Array (My_String_Array);
    for(std::string const& s : My_String_Array) {
        std::cout << s << std::endl;
    }
    system("pause");
    return 0;
}

One more thing: somewhere in your code, you've written something like using namespace std;. Get rid of it. It's bad practice and makes your code harder to interpret for other users ("Is string a custom type or std::string?")

Community
  • 1
  • 1
Xirema
  • 19,889
  • 4
  • 32
  • 68