The malloc() |
int *vector, length, i; ... vector = (int *)malloc(length*sizeof(int)); // but malloc() might fail to allocate assert(vector != NULL); // now we know its safe to use vector[] for (i = 0; i < length; i++) { ... vector[i] ... } |
Alternatively:
int *vector, length, i; ... vector = (int *)malloc(length*sizeof(int)); // but malloc() might fail to allocate if (vector == NULL) { fprintf(stderr, "Out of memory\n"); exit(1); } // now we know its safe to use vector[] for (i = 0; i < length; i++) { ... vector[i] ... } |