|
| 1 | +--- |
| 2 | +title: 2D Malloc |
| 3 | +desc: Malloc a variable-size 2D array of ints! |
| 4 | +class: COMP1511 |
| 5 | +difficulty: 2 |
| 6 | +--- |
| 7 | + |
| 8 | +# 2D Malloc |
| 9 | + |
| 10 | +Daniel has been working on his COMP1511 assignment, but he wants to create a 2D `rows` x `cols` array of integers, where `rows` and `cols` are inputs to the program. |
| 11 | + |
| 12 | +In other words, he wants to create some array `int array[rows][cols]`. |
| 13 | + |
| 14 | +Unfortunately, the style guide for the course does not allow variable length arrays to be created on the stack, so Daniel must `malloc` his array. Of course, this also means he has to `free` all memory afterwards! |
| 15 | + |
| 16 | +Daniel does not know how to dynamically allocate heap memory for a 2D array and free it afterwards, so your job is to implement the functions `allocate_array` and `free_array` in `malloc_two_dimension_array.c`. |
| 17 | + |
| 18 | +- `int **allocate_array(int rows, int cols)` should take in two values, `rows` and `cols`, and should return the start of a heap-allocated array with bounds and behaviour which matches `int array[rows][cols]`. |
| 19 | +- `void free_array(int **array, int rows, int cols)` should take in three values — `array` is the start of the array to be freed, `rows` and `cols` are the dimensions of the array. |
| 20 | + |
| 21 | +The main function has already been included. This main function reads `rows` and `cols` as command line arguments, passes it to `allocate_array`, does some array operations, and then calls `free_array`. **Do not modify this function**, you may create new functions if you wish. |
| 22 | + |
| 23 | +The output should look exactly like the following: |
| 24 | + |
| 25 | +```bash:~/1511-revision/2d_malloc |
| 26 | +$ dcc --leak-check 2d_malloc.c -o 2d_malloc |
| 27 | +$ ./2d_malloc 10 10 |
| 28 | + 0 1 2 3 4 5 6 7 8 9 |
| 29 | + 10 11 12 13 14 15 16 17 18 19 |
| 30 | + 20 21 22 23 24 25 26 27 28 29 |
| 31 | + 30 31 32 33 34 35 36 37 38 39 |
| 32 | + 40 41 42 43 44 45 46 47 48 49 |
| 33 | + 50 51 52 53 54 55 56 57 58 59 |
| 34 | + 60 61 62 63 64 65 66 67 68 69 |
| 35 | + 70 71 72 73 74 75 76 77 78 79 |
| 36 | + 80 81 82 83 84 85 86 87 88 89 |
| 37 | + 90 91 92 93 94 95 96 97 98 99 |
| 38 | +``` |
| 39 | + |
| 40 | +## Assumptions/Restrictions/Clarifications |
| 41 | + |
| 42 | +- You may assume there is enough memory to allocate the array on the heap. |
| 43 | +- You may **not** store the arrays on the stack. |
| 44 | +- You do not need to worry about a horrific eldritch abomination destroying your computer midway through runtime. |
| 45 | + |
| 46 | +## CSE Autotest |
| 47 | + |
| 48 | +When you think your program is working, you can use CSE autotest to test your solution. |
| 49 | + |
| 50 | +```bash:~/1521-revision/2d_malloc |
| 51 | +$ 1511 csesoc-autotest 2d_malloc |
| 52 | +``` |
| 53 | + |
| 54 | +## Solution |
| 55 | + |
| 56 | +You can view the solution code to this problem [here](https://github.com/Allynixtor/comp1511-23T1-problems/blob/main/solutions/2d_malloc/solution.c). |
0 commit comments