3

When I try to run this I get a segmentation fault:

#define FILE_NAME "test.html"
#define STRING_ARRAY_SIZE 1000000

int main() {
fstream file;
string line = "";
string string_array [STRING_ARRAY_SIZE];
int i = 0;

file.open(FILE_NAME);
while(getline(file, line)) {
    string_array[i] = line;
    i++;
    cout << line << endl;
}

file.close();
}

Instead, when I try to compile this, it works:

#define FILE_NAME "test.html"
#define STRING_ARRAY_SIZE 100000

int main() {
fstream file;
string line = "";
string string_array [STRING_ARRAY_SIZE];
int i = 0;

file.open(FILE_NAME);
while(getline(file, line)) {
    string_array[i] = line;
    i++;
    cout << line << endl;
}

file.close();
}

Turns out, the only difference is the size of the array. Why does it work when it is 100000, and it does not when it is 1000000? What is the maximum size? Thank you.

user3289157
  • 645
  • 2
  • 13
  • 24
  • 2
    Local variables, including arrays, are put on the stack, which has a limited size. On Windows using VC++ it's 1MB by default, on Linux it's typically 8MB. Trying to allocate more than that on the stack will cause a *stack overflow* which leads to undefined behavior and most likely a crash. The max number of members in an array are not fixed, but depends on the size of the data and the remaining stack size. – Some programmer dude Jul 23 '14 at 07:17
  • 2
    Do you really get a segmentation fault **during compiling it**? That would mean the compiler has a bug, or something like defective memory... Probably not, but it would be good to make it more clear. – Volker Siegel Jul 23 '14 at 07:32
  • ups you guys are right. Segmentation fault occurs at run time :) – user3289157 Jul 23 '14 at 07:37

4 Answers4

4

The limit is system (not only hardware, but also software, notably operating system and runtime) specific. See also this question very similar to yours.

You should try hard to avoiding too big call stack frames. These days on desktop or server machines, I would recommend at most a few dozen kilobytes for the biggest call stack frames (and very often much less, i.e. hundreds of bytes) - notably for intermediate - non-leaf- or recursive functions. A typical system has a machine stack able to grow to a few megabytes (but on embedded microcontrollers, or inside the Linux kernel, it could be a few kilobytes!). With multi-threaded applications it should be a little less (since each thread has its own stack).

On Linux and Posix systems you can use the setrlimit(2) syscall with RLIMIT_STACK to lower (and perhaps sometimes to slightly increase) the stack limit. In your terminal with a bash shell, use the ulimit -s builtin.

The following GCC options could interest you: -fstack-usage, -Wframe-larger-than=, -fstack-split

In your code, consider replacing

 string string_array [STRING_ARRAY_SIZE];

with

 vector<string> string_vector;

and replacing

string_array[i] = line;
i++;

with

string_vector.push_back(line);
Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
3

There is no limit set by C++ as such but rather the limit is set and dependent on the hardware which you are using. Basically you can say that there are two limits set for the size of the array one that is set by the type(you can use std::size_t to check the size that it can take) of the index used to define array and second is physical memory limit.

Actually the memory is allocated at two places, the first is on the heap (dynamically allocated memory). Here the size limit is basically a combination of available hardware and the OS's ability to simulate space. And the second is on the stack (Locally declared variables).Here the size limit here is compiler defined .

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
1

It could be 2 things.

First of all, you need memory to load things in a program. So, if you don't have enough memory, you will have a Segmentation fault. You cannot reserve 1000000 but you have enough memory to reserve, for example, 100000.

Secondly, take a look to size_t reference. Every object has a limit to represent its data.

mram888
  • 4,899
  • 5
  • 33
  • 59
vgonisanz
  • 11,831
  • 13
  • 78
  • 130
1

First of all: in compile time you cannot have segmentation faults, in compile time it's called "compilation error", and the code above looks correct.

And as always: it depends. There are limitations in the memory stack and it depends on the platform.

Drewen
  • 2,806
  • 1
  • 15
  • 12