-3

Possible Duplicate:
Very large array on the heap (Visual C++)

i need to declare 10 strings each of length 100000 characters long.

int main(void)
{
long t;
cin>>t;
string str[10][100000];
for(long i=0;i<=t;i++)
{
       getline(cin,str[i][100000]);
}

for(long i=1;i<=t;i++)
{
  getStringSize(str[i][100000]);
}
system("PAUSE");
}

i wrote the code in VC++ but as soon as i compile the code i have a stack overflow. if i keep the size of the string to str[10][10000] then the code works great. what do i need to make to code work?

Community
  • 1
  • 1
  • possible duplicate of [Very large array on the heap (Visual C++)](http://stackoverflow.com/questions/3691196/very-large-array-on-the-heap-visual-c), [Is there a max array length limit in C++?](http://stackoverflow.com/questions/216259/216731#216731), [Handling Huge Multidimensional Arrays in C++](http://stackoverflow.com/q/4464670) – Cody Gray - on strike May 24 '12 at 10:52
  • The real question is why are you using C-style arrays if you're writing C++ code? There are much better options; check the `std` namespace. – Cody Gray - on strike May 24 '12 at 10:53
  • 1
    Do not use `system("PAUSE")` as it is platform-dependent and insecure – Attila May 24 '12 at 10:53
  • 2
    What you are doing is declaring a two-dimensional array, of 10*100000 strings, so you have a million strings, not 10 strings. – Zyx 2000 May 24 '12 at 12:00

4 Answers4

4

This is not how you declare ten strings of 10,000 characters each - you declared a 2D array of strings, 10x10000.

This is how you do it the C++ way if you want each string to have 10,000 characters:

vector<string> str(10, string(100000, ' '));

Note that you need to specify the character that you want repeated 10,000 times. Since std::string is designed to grow dynamically, you can skip the 10,000 part altogether: the library will allocate as much memory as it needs.

vector<string> str(10);

You can also use array in C++11.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
2

You need to allocate the memory dynamically: the stack in your case is not big enough to hold that much data

const size_t len = 10;
string* str[len]; 
for(long i=0; i<len; ++i) {
  str[i] = new string[100000];
} 

Note: Don't forget to delete the allocated memory when you no longer need it.

Note: To make life easier, use an appropriate container (e.g. vector<>) that does the memory management for you automatically

Update: your code has some other problems too:

for(long i=0;i<=t;i++) // t could be lager than 9
{ 
  getline(cin,str[i][100000]); // you are accessing a non-existent element
} 

Try instead:

long t;   
cin>>t;   
vector<string> str; // declare an auto-resizing container of strings   

for(long i=0; i<t; i++)
{ 
  string tmp; // this string will be able to store a lot of characters by itself
  getline(cin, tmp); // read in the next line
  str.push_back(tmp); // add the line to our container
} 

for(long i=0; i<t; i++)
{
  // do something with str[i] // values str[0]..str[t-1] are guaranteed to be valid
}
Attila
  • 28,265
  • 3
  • 46
  • 55
0

You need to allocate memory in heap. Like this..

string *str[10];

for(int i = 0; i < 10; i++)
  str[i] = new string[100000];
Ammar
  • 1,947
  • 14
  • 15
  • This allocation happens on *freestore* and not *heap*. – Alok Save May 24 '12 at 10:55
  • @Als: Thanks for correction.. I believe *freestore* and *heap* are used interchangeably. :) – Ammar May 24 '12 at 11:02
  • 1
    Well they are not, allocations made by `new`or `new []` are done on freestore while those by `malloc` are done on heap.Though freestore itself might be implemented as an heap, they both are distinct in a C++ program. – Alok Save May 24 '12 at 11:17
0

If you use std::string then it's not fixed length - that's the whole point. what you maybe want is:

char str[10][100000];

which would give you 10 x char[100000]. As it stands you've got a matrix of 10 x 100000 strings.

Component 10
  • 10,247
  • 7
  • 47
  • 64