11#include < cstdlib>
22
3- void stack_allocation_pointer_arithmetic (int *array) {
3+ void stack_allocated_single_dimensional_pointer_arithmetic (int *array) {
44 /* 1. Pointer formed from performing arithmetic */
55 int *valid1 = array; // COMPLIANT: pointer is within boundary
66 int *valid2 = array + 1 ; // COMPLIANT: pointer is within boundary
@@ -13,7 +13,7 @@ void stack_allocation_pointer_arithmetic(int *array) {
1313 int *invalid2 = array - 1 ; // NON_COMPLIANT: pointer is outside boundary
1414}
1515
16- void stack_allocation_array_access (int *array) {
16+ void stack_allocated_single_dimensional_array_access (int *array) {
1717 /* 2. Array Access (entails pointer arithmetic) */
1818 int valid1 = array[0 ]; // COMPLIANT: pointer is within boundary
1919 int valid2 = array[1 ]; // COMPLIANT: pointer is within boundary
@@ -25,7 +25,7 @@ void stack_allocation_array_access(int *array) {
2525 int invalid2 = array[-1 ]; // NON_COMPLIANT: pointer is outside boundary
2626}
2727
28- void malloc_pointer_arithmetic (int *array) { // [1, 4]
28+ void malloc_single_dimensional_pointer_arithmetic (int *array) { // [1, 4]
2929 /* 1. Pointer formed from performing arithmetic */
3030 int *valid1 = array; // COMPLIANT: pointer is within boundary (lower bound: 1)
3131 int *valid2 = array + 1 ; // COMPLIANT: pointer points more than one beyond the
@@ -41,7 +41,7 @@ void malloc_pointer_arithmetic(int *array) { // [1, 4]
4141 int *invalid3 = array - 1 ; // NON_COMPLIANT: pointer is outside boundary
4242}
4343
44- void malloc_array_access (int *array) { // [1, 4]
44+ void malloc_single_dimensional_array_access (int *array) { // [1, 4]
4545 /* 2. Array Access (entails pointer arithmetic) */
4646 int valid1 =
4747 array[0 ]; // COMPLIANT: pointer is within boundary (lower bound: 1)
@@ -57,7 +57,7 @@ void malloc_array_access(int *array) { // [1, 4]
5757 int invalid2 = array[-1 ]; // NON_COMPLIANT: pointer is outside boundary
5858}
5959
60- void calloc_pointer_arithmetic (int *array) { // [2, 5]
60+ void calloc_single_dimensional_pointer_arithmetic (int *array) { // [2, 5]
6161 /* 1. Pointer formed from performing arithmetic */
6262 int *valid1 = array; // COMPLIANT: pointer is within boundary (lower bound: 2)
6363 int *valid2 =
@@ -72,7 +72,7 @@ void calloc_pointer_arithmetic(int *array) { // [2, 5]
7272 int *invalid2 = array - 1 ; // NON_COMPLIANT: pointer is outside boundary
7373}
7474
75- void calloc_array_access (int *array) { // [2, 5]
75+ void calloc_single_dimensional_array_access (int *array) { // [2, 5]
7676 /* 2. Array Access (entails pointer arithmetic) */
7777 int valid1 = array[0 ]; // COMPLIANT: pointer is within boundary
7878 int valid2 = array[1 ]; // COMPLIANT: pointer is within boundary
@@ -86,7 +86,7 @@ void calloc_array_access(int *array) { // [2, 5]
8686 int invalid2 = array[-1 ]; // NON_COMPLIANT: pointer is outside boundary
8787}
8888
89- void realloc_pointer_arithmetic (int *array) { // [3, 6]
89+ void realloc_single_dimensional_pointer_arithmetic (int *array) { // [3, 6]
9090 /* 1. Pointer formed from performing arithmetic */
9191 int *valid1 = array; // COMPLIANT: pointer is within boundary (lower bound: 3)
9292 int *valid2 =
@@ -100,7 +100,7 @@ void realloc_pointer_arithmetic(int *array) { // [3, 6]
100100 int *invalid2 = array - 1 ; // NON_COMPLIANT: pointer is outside boundary
101101}
102102
103- void realloc_array_access (int *array) { // [3, 6]
103+ void realloc_single_dimensional_array_access (int *array) { // [3, 6]
104104 /* 2. Array Access (entails pointer arithmetic) */
105105 int valid1 =
106106 array[0 ]; // COMPLIANT: pointer is within boundary (lower bound: 3)
@@ -116,14 +116,62 @@ void realloc_array_access(int *array) { // [3, 6]
116116 int invalid2 = array[-1 ]; // NON_COMPLIANT: pointer is outside boundary
117117}
118118
119+ void stack_allocated_multi_dimensional_array_access (int array[2 ][3 ]) {
120+ int valid11 = array[0 ][0 ]; // COMPLIANT: pointer is within boundary
121+ int valid12 = array[0 ][1 ]; // COMPLIANT: pointer is within boundary
122+ int valid13 = array[0 ][2 ]; // COMPLIANT: pointer points one beyond the last
123+ // element, but non-compliant to Rule 4.1.3
124+ int invalid1 = array[0 ][3 ]; // NON_COMPLIANT: pointer points more than one
125+ // beyond the last element
126+
127+ int valid21 = array[1 ][0 ]; // COMPLIANT: pointer is within boundary
128+ int valid22 = array[1 ][1 ]; // COMPLIANT: pointer is within boundary
129+ int valid23 = array[1 ][2 ]; // COMPLIANT: pointer points one beyond the last
130+ // element, but non-compliant to Rule 4.1.3
131+
132+ int invalid2 = array[1 ][3 ]; // NON_COMPLIANT: pointer points more than one
133+ // beyond the last element
134+
135+ int valid31 = array[2 ][0 ]; // COMPLIANT: pointer points one beyond the last
136+ // element, but non-compliant to Rule 4.1.3
137+ int invalid3 = array[3 ][0 ]; // NON_COMPLIANT: pointer points more than one
138+ // beyond the last element
139+ }
140+
141+ void stack_allocated_multi_dimensional_pointer_arithmetic (int array[2 ][3 ]) {
142+ int valid11 = *(*(array + 0 ) + 0 ); // COMPLIANT: pointer is within boundary
143+ int valid12 = *(*(array + 0 ) + 1 ); // COMPLIANT: pointer is within boundary
144+ int valid13 =
145+ *(*(array + 0 ) + 2 ); // COMPLIANT: pointer points one beyond the last
146+ // element, but non-compliant to Rule 4.1.3
147+ int invalid1 = *(*(array + 0 ) + 3 ); // NON_COMPLIANT: pointer points more than
148+ // one beyond the last element
149+
150+ int valid21 = *(*(array + 1 ) + 0 ); // COMPLIANT: pointer is within boundary
151+ int valid22 = *(*(array + 1 ) + 1 ); // COMPLIANT: pointer is within boundary
152+ int valid23 =
153+ *(*(array + 1 ) + 2 ); // COMPLIANT: pointer points one beyond the last
154+ // element, but non-compliant to Rule 4.1.3
155+ int invalid2 = *(*(array + 1 ) + 3 ); // NON_COMPLIANT: pointer points more than
156+ // one beyond the last element
157+
158+ int valid31 =
159+ *(*(array + 2 ) + 0 ); // COMPLIANT: pointer points one beyond the last
160+ // element, but non-compliant to Rule 4.1.3
161+ int invalid3 = *(*(array + 3 ) + 0 ); // NON_COMPLIANT: pointer points more than
162+ // one beyond the last element
163+ }
164+
119165int main (int argc, char *argv[]) {
120- /* 1. Array initialized on the stack */
121- int array [3 ] = {0 , 1 , 2 };
166+ /* 1. Single-dimensional array initialized on the stack */
167+ int stack_single_dimensional_array [3 ] = {0 , 1 , 2 };
122168
123- stack_allocation_pointer_arithmetic (array);
124- stack_allocation_array_access (array);
169+ stack_allocated_single_dimensional_pointer_arithmetic (
170+ stack_single_dimensional_array);
171+ stack_allocated_single_dimensional_array_access (
172+ stack_single_dimensional_array);
125173
126- /* 2. Array initialized on the heap */
174+ /* 2. Single-dimensional array initialized on the heap */
127175 int num_of_elements_malloc;
128176 int num_of_elements_calloc;
129177 int num_of_elements_realloc;
@@ -138,20 +186,34 @@ int main(int argc, char *argv[]) {
138186 num_of_elements_realloc = 6 ;
139187 }
140188
141- int *array_malloc = (int *)malloc (num_of_elements_malloc * sizeof (int ));
142- int *array_calloc = (int *)calloc (num_of_elements_calloc, sizeof (int ));
189+ int *single_dimensional_array_malloc =
190+ (int *)malloc (num_of_elements_malloc * sizeof (int ));
191+ int *single_dimensional_array_calloc =
192+ (int *)calloc (num_of_elements_calloc, sizeof (int ));
193+
194+ int *single_dimensional_array_realloc = (int *)realloc (
195+ single_dimensional_array_malloc, num_of_elements_realloc * sizeof (int ));
196+
197+ malloc_single_dimensional_pointer_arithmetic (single_dimensional_array_malloc);
198+ malloc_single_dimensional_array_access (single_dimensional_array_malloc);
199+
200+ calloc_single_dimensional_pointer_arithmetic (single_dimensional_array_calloc);
201+ calloc_single_dimensional_array_access (single_dimensional_array_calloc);
143202
144- int *array_realloc =
145- (int *)realloc (array_malloc, num_of_elements_realloc * sizeof (int ));
203+ realloc_single_dimensional_pointer_arithmetic (
204+ single_dimensional_array_realloc);
205+ realloc_single_dimensional_array_access (single_dimensional_array_realloc);
146206
147- malloc_pointer_arithmetic (array_malloc);
148- malloc_array_access (array_malloc) ;
207+ /* 3. Multi-dimensional array initialized on the stack */
208+ int stack_multi_dimensional_array[ 2 ][ 3 ] = {{ 1 , 2 , 3 }, { 4 , 5 , 6 }} ;
149209
150- calloc_pointer_arithmetic (array_calloc);
151- calloc_array_access (array_calloc);
210+ /* 4. Multi-dimensional array initialized on the heap */
211+ int (*heap_multi_dimensional_array)[3 ] =
212+ (int (*)[3 ])malloc (sizeof (int [2 ][3 ]));
152213
153- realloc_pointer_arithmetic (array_realloc);
154- realloc_array_access (array_realloc);
214+ stack_allocated_multi_dimensional_array_access (stack_multi_dimensional_array);
215+ stack_allocated_multi_dimensional_pointer_arithmetic (
216+ stack_multi_dimensional_array);
155217
156218 return 0 ;
157219}
0 commit comments