Issues with function - please help

Hello everyone,

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <string>
#include <sstream>
#include <cmath>
#include <cctype>
#include <cstring>
#include <fstream>
using namespace std;

char word[40][235887];

void dictionaryload()
{
    cout << "Importing dictionary from file...";
    char dictstream [9435480];
    long int i =0; // index of dictstream
    long int 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 


Thanks so much,
Ryan
Last edited on
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.
Last edited on
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.
Last edited on
If you're forced to use arrays for a school assignment you need to ask your teacher how to allocate more space on the stack for it to work.
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?

Hope all goes well.
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.

Thanks,
Ryan
Do you think malloc might also help? apparently there is no limit to the amount of ram it can allocate, other then the OS.

Thanks
You can find all about on the reference pages - top left of this page. Or google C++ std::set example

As I said map might be better if you want to store 2 values.

I dont really want to use string class because it doesnt play nicely with the encryptor and decryptor.


Why is that?
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;


Also C++ strings can be converted to cstrings.

http://www.cplusplus.com/reference/string/string/c_str/
Topic archived. No new replies allowed.