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
#include <stdlib.h>
void *malloc(size_t size)
size
bytes and returns a pointer to the
allocated memory. The memory is not initialized.void free(void *ptr)
ptr
.void *calloc(size_t nmemb, size_t size)
nmemb
elements of
size
bytes each and returns a
pointer to the allocated memory. The memory is set to zero.void *realloc(void *ptr, size_t size)
void *reallocarray(void *ptr, size_t nmemb, size_t size);
nmemb
elements, each of which is
size
bytes.const int size = 1234; // any value determined at runtime
int array[size];
Unfortunately, the above only works for C99 capable compilers
const int size = 1234; // any value determined at runtime
int* array = malloc(size * sizeof(int));
// use the array ...
free(array);
Alternatively, one can be more explicit with a typecast
const int size = 1234; // any value determined at runtime
int* array = (int*) malloc(size * sizeof(int));
// use the array ...
free(array);
const size_t size = 1234; // any value determined at runtime
int* array = (int*) malloc(size * sizeof(int));
if (array == NULL) { // NULL pointer means no memory was allocated
fprintf(stderr, "malloc %lu bytes failed\n", size);
exit(EXIT_FAILURE);
}
// use the array ...
free(array);
Determine the size of the stack via
$ ulimit -s # or `ulimit -a` for all limits
Impossible to exceed the given stack size
#include <stdio.h>
int main(void) {
int array[8192 * 1024 / 4]; // use 8192 kB if sizeof(int) = 4
printf("Survived?\n");
return 0;
}
Try to compile and run exceed_stack_size.c
If large amount of memory is needed, one has to use the heap...
#include <stdio.h>
#include <stdlib.h>
int main(void) {
const size_t size = 12; // any value determined at runtime
int* array = (int*) malloc(size * sizeof(int));
if (array == NULL) { exit(EXIT_FAILURE); }
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 ...
printf("array[%lu]: %d\n", i, array[i]);
}
free(array); // otherwise memory will grow when the program keeps running
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main(void) {
size_t size = 8192 * 1024; // 8 MB
int* array = (int*) malloc(size);
if (array == NULL) {
fprintf(stderr, "malloc %lu bytes failed\n", size);
exit(EXIT_FAILURE);
}
for (size_t i = 0; i < size / sizeof(int); ++i) {
array[i] = i * i; // initialize the array ...
}
printf("Survived?\n");
free(array); // avoid memory leak
return 0;
}
Download allocate_large_array.c
#include <stdio.h>
#include <stdlib.h>
typedef struct point { double x; double y; } Point;
Point* fetch_points() {
Point* points = (Point*) malloc(2 * sizeof(Point));
points[0] = (Point) { .x=4, .y=7 };
points[1] = (Point) { .x=-5, .y=3 };
return points;
}
int main(void) {
Point* points = fetch_points();
if (points == NULL)
fprintf(stderr, "no memory allocated\n"); exit(EXIT_FAILURE);
printf("Point 1: %5.2lf / %5.2lf\n", points[0].x, points[0].y);
printf("Point 2: %5.2lf / %5.2lf\n", points[1].x, points[1].y);
free(points);
return 0;
}