0

I am trying to write each unique word from a text file into a struct of unique words I'm not quite that far yet, but I am getting a segmentation fault when trying to write values into my array of structs.

We cannot use pointers as we haven't learned about them yet, and we have to use using namespace std.

I am fine figuring out how to use loops for the unique word portion, I just need help figuring out how to fix the segmentation fault. I have narrowed it down to where the segmentation fault starts at the for loop in the do-while loop, but I don't know where to go from there and I've found nothing online to help.

#include <iostream>
#include <fstream>
#include <cmath>
#include <string>

using namespace std;

struct UniqueWord{
    string word;
    int numOccurences;
};

int main(){

    const int N = 100000;
    ifstream CleanedText;
    CleanedText.open("testTextCleaned.txt"); //change to proper text later
    string word1;
    string words[N];
    for(int i = 0; i < N; i++){
        CleanedText >> words[i];
    }
    CleanedText.close();
    CleanedText.open("testTextCleaned.txt"); //change to proper text later
    UniqueWord wordarray[N];
    for(int i = 0; i < N; i++){
        CleanedText >> word1;
        do{
            for(int j = 0;j<N+1;j++){
                wordarray[j].word = word1;
            }
        }while(words[i] != word1);

    } 

    return 0;
}

I expect to be able to put each word from the original file into the array of structs.

M P
  • 13
  • 2
  • You probably have a stack overflow at `string words[N];` Your array is way too big to live on the stack – Guillaume Racicot Apr 26 '19 at 14:32
  • 5
    Look at `for(int j = 0;j – NathanOliver Apr 26 '19 at 14:32
  • 6
    [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Jesper Juhl Apr 26 '19 at 14:34
  • 2
    To further comment on what @NathanOliver said, remember an N-size array has valid indices from 0 to N-1. array[N] is one past the end. – xaxxon Apr 26 '19 at 14:36
  • I don't notice anything about the end condition, but I've reduced the size of the array and now it is running forever. No segmentation fault, though!However, the final file I need to run this on is about 100,000 words long so I need an array that big. – M P Apr 26 '19 at 14:39
  • Why do you need to store the whole file in memory? Read one word, process one word, repeat. – Thomas Sablik Apr 26 '19 at 14:42
  • The size of `std::string` varies from compiler implementation to implementation (and sometimes even between different versions of the same compiler). It could be as little as 8 bytes (32 bit pointer and 32 bit size counter), but it often contains a small array to speed up short strings so you could have something upwards of 32 bytes. 32 * 100,000 is greater than the 1 MB stack space provided as the default on a Windows PC. Keep an eye on it. – user4581301 Apr 26 '19 at 14:47
  • @user4581301, thank you I did not know that! – M P Apr 26 '19 at 14:49
  • Instead of an array you should use a [std::set](https://en.cppreference.com/w/cpp/container/set) to ensure uniqueness or [std::map](https://en.cppreference.com/w/cpp/container/map) to also count the words. – Thomas Sablik Apr 26 '19 at 14:51

1 Answers1

3

Lose the +1 on the end condition.

        for(int j = 0;j<N/*+1*/;j++){
            wordarray[j].word = word1;
        }

J needs to go from 0 through N-1. Say N is 2, wordarray[0] and wordarray[1] are valid. wordarray[2] is not.

xaxxon
  • 19,189
  • 5
  • 50
  • 80