...
This non-compliant example demonstrates the incorrect way to declare a jagged triangular array of 100 x 100 integers.
| Code Block | ||
|---|---|---|
| ||
/* assuming 32-bit pointer, 32-bit integer */ int i; int** intarraytriarray = calloc(100, 4); for (i = 0; i < 100; i++) intarraytriarray[i] = calloc(100i, 4); |
Compliant Solution
The above example can be fixed by replacing the hard-coded value 4 with the actual size of the datatype as represented on the target platform. Remember to check the return value of the memory allocation routines.
| Code Block | ||
|---|---|---|
| ||
int i; int** intarraytriarray = calloc(100, sizeof(int*)); if (!intarraytriarray) { /* perform cleanup, return error */ } for (i = 0; i < 100; i++) { intarraytriarray[i] = calloc(100i, sizeof(int)); if (!intarraytriarray[i]) { /* perform cleanup, return error */ } } |
...