I am trying to create a kind of table with random characters and writing the into a file, but I have a problem. The problem appears when I want to create huge tables. For example for a table with 10 rows x 5 columns everything is perfect, but for a table with 1.000.000 rows and 100 columns I have errors. Do you have any idea how could I solvent th开发者_Python百科is problem???
I attached you the code I have created:
#include<iostream>
#include<fstream>
#include<stdio.h>
#include<string>
#include<ctime>
#include<sstream>
using namespace std;
int main()
{
int rows, columns, rowsMax, columnsMax;
int element1;
char word[10];
cout<<"Write the number of rows of your table: ";
cin>>rowsMax;
cout<<"Write the number of columns of your table: ";
cin>>columnsMax;
string matriz[100000][5]; // Here I write the number of row and columns that must be the same than the numbers introduced as input
string table ("Table1");
ofstream myfile (table);
if(myfile.is_open())
srand(1);
for(rows=0;rows<rowsMax;rows++)
{
for(columns=0;columns<columnsMax;columns++)
{
element1 = rand() % 100000 + 1;
int len = rand () % 4 + 4;
word [len] = 0;
while (len) word [--len] = 'A' + rand () % 58;
myfile<<element1<<word;
myfile<<"|";
std::stringstream ss;
ss<<element1;
string mat;
mat +=ss.str();
mat +=word;
matriz[rows][columns]= mat;
}
myfile<<endl;
}
myfile.close();
system("pause");
return 0;
}
Thanks in advance for all your help!
You have allocated the array on the stack which is usually have very limited size. For Windows it is about 1Mb by default.
You can allocate the array on the heap instead.
A table with 1.000.000 rows and 100 columns:
string matriz[1000000][100];
means 1000000*100*sizeof string >1MB , which is huge compared to the default thread stack size. Even if you allocate the matrix using heap the size would be around more than 2GB ( default process Virtual memory).
Probably memory mapped mechanism would help!
1000000 rows and 100 columns is an awful lot of data (100 million of anything is going to take a lot of storage). Perhaps you don't have enough memory?
Is every row and column populated? If not, then you might be able to save something by using a sparse matrix representation.
You're creating your matrix on the stack, which has a limited size. Try to allocate it on the heap, using "new", or a vector of strings.
If you are working with huge matrix you should use linked list. example
精彩评论