Iam having a bit of an issue with a function and I was wondering if anyone would be able to help me. The function is meant to read in an entire massive file to a 1d character array, then convert it into a 2d array by the space character.
#include <iostream>
#include <string>
#include <sstream>
#include <cmath>
#include <cctype>
#include <cstring>
#include <fstream>
usingnamespace std;
char word[40][235887];
void dictionaryload()
{
cout << "Importing dictionary from file...";
char dictstream [9435480];
longint i =0; // index of dictstream
longint w = 0; // word count
int l = 0; // letter count
ifstream fin;
fin.open("dicthash.txt");
fin.getline(dictstream,9435480);
while (dictstream[i] != 0)
{
if (dictstream[i] != ' ')
{
word[l][w] = dictstream [i];
l++;
i++;
}
else
{
l = 0;
i++;
w++;
}
}
cout << "Complete!";
}
As soon as the function is called, the program crashes, without even getting to the cout statement at the top of the function. I don't know why this happens as the line before it executes properly. I have tried declaring the function with or without 'void' in the brackets.
Here is where it is called:
1 2
cout << "Program will guess the encryption key and decrypt." // this line executes << endl;
dictionaryload(); // seems to crash either here or at the start of the function
char dictstream [9435480];
Chances are, this line is happening before the cout statement even though it is written after. That's a massive amount of memory on top of what you already have, I'd assume it's a stack overflow you're getting.
To fix it, you'll need to use the heap (new[] and delete[]) - the stack just isn't cut out for piling up that high in the air without overflowing.
Since you are using C++ I recommend using a std::vector or one of the other standard containers instead of the arrays, for both dictstream and word, and since your sizes are so large I recommend that you use size_t for the index and count variables.
I would use the STL std::set, if you are allowed to. This is an efficient container that uses an AVL tree, and it is sorted. The find operation will be very fast compared to the other containers.
std::map is good too, you can store a word and a meaning of the word say.
I would not use a vector for large amounts of data.
Why can't you read your input by words, rather than by individual chars?
I would prefer to work with words, but I couldnt find a way before without using string class... I dont really want to use string class because it doesnt play nicely with the encryptor and decryptor.
How would I write this with std::set? I have never used it before.
I wrote it the first time using string class, but the program involves a lot of typecasing from a letter in the string, to an integer, and back to the string (thats how the encryption works, it modifies the ascii code based on what type of encryption and the key). I was having lots of unexplained errors with string type, such as values what shouldn't be there and trouble converting the numbers back into string class. I dont know why it was doing this, but a friend suggested I just use character arrays. So I re wrote the entire thing using character arrays (not char*) and it worked properly
What would the 2 values be? would it be like a 1d array of string in that you have an array of words and an array of characters inside each word?
When I removed one of the digits in dictstream (making the array 10x smaller) it did get to the first cout statement, so I would assume that means the issue is the stack overflow, as everyone said before.
Why is it that the variable gets declared before the cout statement, even though it is after it in the code? I was under the impression programs always run sequentially.
Since cout is buffered it doesn't print until the buffer if flushed. And since you don't flush the buffer before the error it doesn't print. Try adding an endl or flush to your cout statement.
Ok, no worries. If you only need to store one thing, then use set.
Map is handy if you have a key and a value.
It works like a look up function, key is the search argument, and it's corresponding value is returned.
I wrote it the first time using string class, but the program involves a lot of typecasing from a letter in the string, to an integer, and back to the string
A char is just a small int, so no need for casting. You can arithmetic on them directly:
1 2 3 4
char MyChar = 'a';
char Answer = MyChar + 2; //Answer is 'c'
//or this way
Answer = 'a' + 2;