Question: Consider the following C code that uses dynamically allocated 2D array of integers to store a matrix. Find at least 5 problems that would result
Consider the following C code that uses dynamically allocated 2D array of integers to store a matrix.
Find at least 5 problems that would result in a compile-time problem (warning or error), or runtime problems (crahes or undefined values).
Describe each problem briefly and feel free to draw arrows to relevant parts of the code.
Do not include code style issues such as variable naming choices or indentation
#include
#include
void init2DArray(int **arr, int rows, int cols) {
int **tmpArr = NULL;
for(int i = 0; i < cols; i++) {
tmpArr[i] = (int*) malloc( sizeof(int) * cols); }
int cnt = 0;
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
*(*(tmpArr + i) + j) = cnt; ++cnt; } }
*arr = tmpArr; }
int freeArray(int **arr) { free(arr); }
int main(int argc, char **argv) {
int **2DArray = NULL;
printf("Number of rows? ");
scanf("%d", rows);
init2DArray(&2DArray, rows, 10);
freeArray(2DArray); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
