c++pointersstructaccess-violation

I get a writing access violation of 0x00000... when trying to assign a value into my struct


for part of a school lab I need to read in unique words and their corresponding count with a struct. I am new to structs so please bear with me. I am getting an access violation when I try to write the adress of the current word to the character pointer inside of the current instance of my struct. I have read that this is due to dereferencing a nullptr. I have tried to understand this, but I just don't get it. I have resized arrays just like this on regular char** arrays for accepting new words. I am at a loss, any help would be greatly appreciated. The input file used here is just random words separated by non letter characters but not - or , Here is my code:

#define _CRT_SECURE_NO_WARNINGS
#define _CRTDBG_MAP_ALLOC
#include <iostream>
#include <iomanip>
#include <fstream>
#include <limits>

using std::cin;
using std::cout;
using std::endl;
using std::setw;
using std::right;
using std::left;

using std::ifstream;
using std::ofstream;

const int BUFFER = 100; //I figure this buffer is big enough for any given word

struct Word_Count_STRUCT
{
    char* WORD = nullptr;
    int COUNT = 0;
};

int main()
{
    _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);

    //Input for phrase
    ifstream iphrase;

    //Output to CSV (word count)
    ofstream o_count;

    //Word Exceptions
    ifstream xinWord;


    char wordbuffer[BUFFER] = { '\0' };
    char ch = 0;
    Word_Count_STRUCT** uniquewords = nullptr;
    Word_Count_STRUCT** temp = nullptr;

    int k = 0;
    int wordcount = 0;
    char* cword = nullptr; //Current Word
    bool NextWord_flag = false;
    bool interwordpunct = false;
    bool NewWord_flag = true;


    iphrase.open("C:\\Users\\me\\Desktop\\henroE.txt");
    if (iphrase.is_open())
    {
        while (!iphrase.eof())
        {
            
            iphrase.get(ch);

            if (isalpha(ch) || ch == '\'' || ch == '-')
            {
                wordbuffer[k] = ch;
                ++k;
                NextWord_flag = true;

                if (ch == '\'' || ch == '-')
                    interwordpunct = true;
            }
            if ( (NextWord_flag == true) && (!isalpha(ch)) && (interwordpunct == false) )
            {
                k = 0;
                cword = new char[strlen(wordbuffer) + 1];
                strcpy(cword, wordbuffer);
                memset(wordbuffer, '\0', sizeof(wordbuffer));
                for (int i = 0; (i < wordcount) && (NewWord_flag == true); ++i)
                {
                    
                    int cmp = _stricmp(uniquewords[i]->WORD, cword);
                    if (cmp == 0)
                    {
                        NewWord_flag = false;
                        uniquewords[i]->COUNT++;
                        delete[] cword;
                    }
                }
                if (NewWord_flag == true)
                {
                    temp = new Word_Count_STRUCT * [wordcount + 1]();
                    for (int i = 0; i < wordcount; ++i)
                    {
                        temp[i] = uniquewords[i];
                    }
                    delete[] uniquewords;

                    temp[wordcount]->WORD = cword;
                    temp[wordcount]->COUNT++;
                    uniquewords = temp;
                    ++wordcount;
                    NextWord_flag = false;
                }
                interwordpunct = false;
                NewWord_flag = true;


            }

        }
}

I get an error on this line:

temp[wordcount]->WORD = cword;

I also get an error on the int value COUNT as well if I comment the line above it out. So I am guessing it is something with how I initialized the struct. Worth noting that if I do not initialize this call:

temp = new Word_Count_STRUCT * [wordcount + 1]();

and instead just leave it as

temp = new Word_Count_STRUCT * [wordcount + 1];

I get another access violation but for reading instead of writing at 0xFFFFF...

At a loss, thank you for any help :)


Solution

  • You've got a number of things wrong. First, using fixed-length character buffers instead of C++ strings is about 20 years out of date and WILL cause buffer overflow errors unless you are exceedingly careful.

    But this is an issue:

                    temp = new Word_Count_STRUCT * [wordcount + 1]();
                    for (int i = 0; i < wordcount; ++i)
                    {
                        temp[i] = uniquewords[i];
                    }
                    delete[] uniquewords;
    

    But where did you allocate uniquewords? You declared it.

    You also allocate cword outside a loop but the delete it inside a loop -- which seems really fishy, too.

    But note that all you've allocated are pointers. I don't see you actually allocating the structure you're trying to put data in.