
#include <stdio.h>
#include <string.h>
int main()
{
char arr[1500000000];
int i;
for(i=0;i<sizeof(arr);++i)
arr[i]='t';
}



msbachman wrote:So I'm guessing you're getting seg faults?
I coded this just now and got a segmentation fault when I tried to run it:
- Code: Select all
#include <stdio.h>
#include <string.h>
int main()
{
char arr[1500000000];
int i;
for(i=0;i<sizeof(arr);++i)
arr[i]='t';
}
Solution's easy enough though, that's not how you should do it. If you're using a wordlist, you can read in a line (or several even) into a smaller array and work with it however you need to.
If it's something like programming one, like you said, I'd sort one of the words (that you fetch from the web page, not from the word list), and sort and search through each of the words (in the wordlist) in turn in an array gotten by something like fgets.
This might be slower (and you're going to have to do a rewind(FILE * stream) after you find each word), but it should solve the problem you're having.
Good luck.

ColdwaterQ wrote:P.S. I am using bubble search and Selection Sorting so it actually worked pretty fast on the 1000 word wordlist and 10 words.
#include <vector>
#include <fstream>
#include <algorithm>
int main(int argc, char **argv)
{
std::vector<std::string> v;
std::fstream f ("test.txt", std::fstream::in);
while (!f.eof())
{
std::string s;
std::getline(f, s);
v.push_back(s);
}
std::sort(v.begin(), v.end());
// if you wish to do something with the ordered results
for (std::vector<std::string>::iterator it = v.begin();
it != v.end();
++it
)
{
// do something with the results in order
}
// or if you wish to perform a binary search on the orderd results
// NOTE: this assumes all values are unique. if not it's no biggy
std::string t = "some string to match";
std::string::iterator it = std::binary_search(v.begin(), v.end(), t);
if (*it = t)
{
// we have an exact match in O(log n) time
}
}
#include <set>
#include <fstream>
int main(int argc, char **argv)
{
std::set<std::string> st;
std::fstream f ("test.txt", std::fstream::in);
while (!f.eof())
{
std::string s;
std::getline(f, s);
st.insert(s);
}
// sort is done on insertion so no need to sort it again.
// if you wish to do something with the ordered results
for (std::set<std::string>::iterator it = st.begin();
it != st.end();
++it
)
{
// do something with the results in order
}
// Traverse the tree to search in O(n) time
std::string t = "some string to match";
std::set<std::string>::iterator it = st.find(t);
if (it != st.end())
{
// we found a match in O(n) time
}
}



char array[10000000];
char *array = new char[10000000];

Users browsing this forum: No registered users and 0 guests