Access violation exception being thrown C++

0

This program is to create a Library that is 1024 lines deep and a List that is 128 lines deep (both arrays). The contents of each line for both arrays will be chars between 2MB to 3MB in length (randomly assigned by program). The user will be prompted to type in a random word and the program will search for all instances of that word in the List. The line(s) that contain the word will remain in the list, those that do not contain the word will be ejected from List and be reinitialized at the bottom of the Library. Simultaneously, the List will be truncated and the upper-most lines of the Library will be added to the empty lines of the List. The code compiles and runs but I get some bugs. I'm getting an exception thrown in the print_list() function when it runs and I search a char: 0xC0000005: Access violation reading location 0xFDFDFDFD.

#include <iostream> 
#include <cstdlib>
#include <ctime>
#include <cstring>
using namespace std;

// Constants used to create depth and width of recent_list and Library
const int recentList_Size = 10, library_Size = 15; // eventually will use 128 and 1024
const int Low = 20, High = 30; // eventually will be 2MB and 3MB

// Create pointers for recent_list and Library
struct Arrays {
    char* recent[recentList_Size];
    char* library[library_Size];
};

// Method to generate 26 CAPITAL letters using ascii value
inline char r_Letters() {
    return char('A' + rand() % 26);
}

// Build the array
void initArrays(Arrays& o) {
for (int i = 0; i < recentList_Size; i++)  // Build recent_list vertically
{
    int sz = rand() % (High - Low) + Low;  // Used to randomly generate array sized between 2MB and 3MB
    o.recent[i] = new char[sz + 1];        // Dynamically allocate memory to a pointer based on list size
    for (int j = 0; j < sz; j++)
        o.recent[i][j] = r_Letters();      // Fill horizontal array with randomly generated alphabet characters 
    o.recent[i][sz] = '\0';
}
for (int i = 0; i < library_Size; i++)     // Build Library (simialr method as the above)
{
    int sz = rand() % (High - Low) + Low;
    o.library[i] = new char[sz + 1];
    for (int j = 0; j < sz; j++)
        o.library[i][j] = r_Letters();
    o.library[i][sz] = '\0';
}
}

// Function used to print recent_list and Library
void print_list(Arrays* a) {
cout << "Printing Lists..." << endl;

cout << "Recent List: " << endl;
for (int i = 0; i < recentList_Size; ++i)
    cout << '\t' << a->recent[i] << '\n';

cout << "Library: " << endl;
for (int i = 0; i < library_Size; ++i)
    cout << '\t' << a->library[i] << '\n';
}

int main() {
srand(time(0)); // seed rng
Arrays a;
initArrays(a);
int input;

// Prompt for user
cout << "----------------------------------------------" << endl;
cout << " Press 1 to search the Recent List for a word " << endl;
cout << " Press 2 to display Recent List and Library " << endl;
cout << " Press 3 to exit the program" << endl;
cout << "----------------------------------------------" << endl;
cin >> input;

// catch if input not within bounds
while (input != 1 && input != 2 && input != 3)
{
    cout << "-------- Please ONLY press 1, 2 or 3 ---------" << endl;
    cout << " Press 1 to search the Recent List for a word " << endl;
    cout << " Press 2 to display Recent List and Library " << endl;
    cout << " Press 3 to exit the program" << endl;
    cout << "---------------------------------------------" << endl;
    cin >> input;
}

while (input != 3)
{
    if (input == 1) {
        // print_list(&a); // used for testing purposes | displays both lists of characters
        cout << "Enter a word to search for in the recent_list: ";

        char* search_word = new(char[11]); // 10 letter word, 11 character of \0 to mark the end of the string
                                               // Dynamic memory used for comparison
        cin >> search_word;

        while (cin.get() != '\n') continue; // FLush buffer 

        cout << "Searching for: \"" << search_word << "\"\n"; // used for testing purposes

        // Array of booleans that will match the recentList length, where we are going
        // to mark if the array has word we are looking for
        bool words_found_at[recentList_Size] = { false };

        // Go through each array of letters and find the matching word
        int words_found = 0; // Keep count of instances word is found in each array of letters
        for (int i = 0; i < recentList_Size; i++) {
            // C++ function strstr used to compare chars of array to char inputted 
            if (strstr(a.recent[i], search_word) != NULL) {
                words_found++;            // increment counter
                words_found_at[i] = true; // Tab when word is found
            }
        }

        cout << search_word << " found in " << words_found << " arrays " << endl;

        // "Move" the arrays where the word was not found to a temp one
        char** recent_list_move = new (char* [words_found]);
        for (int i = 0, j = 0; i < recentList_Size; i++) {
            // instances in char array where word was not found
            if (words_found_at[i] == false) {
                recent_list_move[j] = a.recent[i]; // create temporary array of chars w/o word based on array position
                j++;
            }
        }

        // "Move" the arrays from the second queue which has the size of the words found
        char** library_list_move = new(char* [words_found]);
        for (int i = 0; i < words_found; i++) {
            library_list_move[i] = a.library[i];
        }

        // Shift Library up to maintain the FIFO order
        // j is used for the position of array after the top n arrays have been swapped to the recent_list
        for (int i = 0, j = words_found; i < library_Size; i++, j++) {
            a.library[i] = a.library[j];
        }

        // "Move" arrays of words not found from temp to the Library
        // Library_size - words_found gives the number of spaces needed to be filled
        for (int i = library_Size - words_found, j = 0; i < library_Size; i++, j++) {
            a.library[i] = recent_list_move[j];
        }

        // Find first index where word(s) have not been found, since that is the index
        // Keeps count of how many spaces need to be filled
        int shift_starts_at = 0;
        for (int i = 0; i < recentList_Size; ++i) {
            if (words_found_at[i] == false) {
                shift_starts_at = i;
                break;
            }
        }

        // Truncate recent_list up 
        for (int i = shift_starts_at, j = shift_starts_at; i < recentList_Size; i++) {
            if (words_found_at[i] == true) {
                a.recent[j] = a.recent[i];
                j++;
            }
        }

        // Fill in the recent_list with contents from library based on count
        // i will be the starting position after truncation | j will be used for the upper contents of the Library
        for (int i = recentList_Size - words_found, j = 0; i < recentList_Size; i++, j++) {
            a.recent[i] = library_list_move[j];
        }

        print_list(&a);
    }

    else // If user enters 2, display up-to-date recent_list
    {
        print_list(&a);
    }

    cout << "----------------------------------------------" << endl;
    cout << " Press 1 to search the Recent List for a word " << endl;
    cout << " Press 2 to display Recent List and Library " << endl;
    cout << " Press 3 to exit the program" << endl;
    cout << "----------------------------------------------" << endl;
    cin >> input;

    while (input != 1 && input != 2 && input != 3)
    {
        cout << "-------- Please ONLY press 1, 2 or 3 ---------" << endl;
        cout << " Press 1 to search the Recent List for a word " << endl;
        cout << " Press 2 to display Recent List and Library " << endl;
        cout << " Press 3 to exit the program" << endl;
        cout << "---------------------------------------------" << endl;
        cin >> input;
    }

}
return 0;
}
c++
arrays
exception
struct
dynamic-memory-allocation
asked on Stack Overflow Jan 28, 2020 by Fab J • edited Feb 1, 2020 by Fab J

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0