Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maximum size of local array variable

Tags:

c++

arrays

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.

like image 891
user3289157 Avatar asked Sep 17 '26 02:09

user3289157


2 Answers

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);
like image 99
Basile Starynkevitch Avatar answered Sep 18 '26 16:09

Basile Starynkevitch


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 .

like image 24
Rahul Tripathi Avatar answered Sep 18 '26 14:09

Rahul Tripathi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!