The size of the stack can be determined with ulimit -s
Some of these problems are (partly) solved by higher level languages
For example, C++ offers containers that manage memory for the programmer
new
delete
new[]
delete[]
Type* var_name = new Type; // allocate memory and store pointer in `var_name`
delete var_name; // free memory
Type* array_name = new Type[num_elements]; // allocate memory
delete[] array_name;
Determine array size at runtime
const int size = 1234; // any value determined at runtime
int array[size];
Unfortunately, the above only works for C99 capable compilers
ISO C++ does not support this (although some compilers do)
const int size = 1234; // any value determined at runtime
int* array = new int[size];
// use the array ...
delete[] array;
No need to check if array == nullptr
when the allocation
fails.
C++ would throw an exception in that case.
Determine the size of the stack via
$ ulimit -s # or `ulimit -a` for all limits
Impossible to exceed the given stack size
#include <iostream>
int main(void) {
int array[8192 * 1024 / 4]; // use 8192 kB if sizeof(int) = 4
std::cout << "Survived?\n";
return 0;
}
Try to compile and run exceed_stack_size.cpp
If large amount of memory is needed, one has to use the heap...
#include <iostream>
int main(void) {
const size_t size = 12; // any value determined at runtime
int* array = new int[size]; // array on the free store
int array_on_stack[3]; // array on stack (see visualization!)
for (size_t i = 0; i < size; ++i) {
array[i] = i * i; // initialize the array ...
}
for (size_t i = 0; i < 3; ++i) { // use the array ...
std::cout << "array[" << i << "]: " << array[i] << std::endl;
}
delete[] array; // otherwise memory will grow when the program keeps running
return 0;
}
#include <iostream>
int main(void) {
size_t size = 8192 * 1024; // 8 M * 4 B
int* array = new int[size]; // 32 MB
for (size_t i = 0; i < size / sizeof(int); ++i) {
array[i] = i * i; // initialize the array ...
}
std::cout << "Survived?\n";
delete[] array; // avoid memory leak
return 0;
}
Download allocate_large_array.cpp
#include <iomanip>
#include <iostream>
struct Point { double x; double y; };
Point* fetch_points() {
Point* points = new Point[2];
points[0] = (Point) { .x=4, .y=7 };
points[1] = (Point) { .x=-5, .y=3 };
return points;
}
int main(void) {
Point* points = fetch_points();
std::cout << std::fixed << std::setprecision(2);
std::cout << "Point 1: " << std::setw(5) << points[0].x;
std::cout << " / " << points[0].y << std::endl;
std::cout << "Point 2: " << points[1].x << " / " << points[1].y << std::endl;
delete[] points;
return 0;
}