Please post to this thread your solution to the Nov 2 In-Class Exercise.
Best,
Chris
(Edited: 2021-11-08)<nowiki>
How much memory allocated:
it takes 4 bytes for an int there are 3 rows of 4 ints 344 = 48 bytes
How would the values be written in the memory:
it would be stored in the memory row by row
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
the individual bytes for a[0][1] 00000000|00000000|00000000|00000000|00000000|00000000|00000000|00000001
</nowiki>
(Edited: 2021-11-03)<pre> int a[3][4] = { {0, 1, 2, 3}, {4, 5, 6, 7}, {8, 9, 10, 11} } The C compiler would store 3 * 4 ints, which would be 4 bytes each 3 * 4 * 4 = 48 bytes total. The values would be written one row after another: {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11} </pre>
(Edited: 2021-11-03)For the array int a[3][4]:
The amount of memory that the C compiler would allocate for the array is: (# columns) * (# rows) * 4 bytes = 48 bytes
The values would be written in memory as the following: 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11
00000000000000000000000000000000|00000000000000000000000000000001|...
(Edited: 2021-11-03)Mem alloc in C:
For an array Size of an int in C is 4 bytes
Memory = 3 * 4 * 4 = 48 bytes
Since this in an array there would be a contiguous series of 48 bytes allocated to this array This array would be written
a[0][0] a[0][1] a[0][2] a[0][3] a[1][0] a[1][1] a[1][2] a[1][3] a[2][0] a[2][1] a[2][2] a[2][3]
in integers: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
for a[0][0] the bytes would would be 0000 0000 0000 0000 0000 0000 0000 0000
(Edited: 2021-11-03)A single int is 4 bytes. In a[3][4] there are 3 rows by 4 columns. 4 bytes * 3 * 4 = 48 bytes. The values are written in memory in order by consecutive rows: {0 1 2 3 4 5 6 7 8 9 10 11}
(Edited: 2021-11-03)For C, it will define ints as 4 bytes, we have 34 ints so in total we have 124 bytes allocated so in total the array will take up 48 bytes of memory
Since all elements of the array are statically defined it will create one 48 byte block of memory and flatten the array into a single dimension by placing each row in front of the previous: [0,1,2,3,4,5,6,7,8,9,10,11] with each element taking up 4 bytes. in memory it would be [00000000|00000000|000000000|00000000|00000000|00000000|00000000|0000001|...]
(Edited: 2021-11-03)<nowiki> Each int is 4 bytes 48 bytes total for the array
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 </nowiki>
(n * m) * sizeOf(T), n = 3, m = 4, T = integer (3 * 4) * 4 bytes = 12 * 4 bytes = '''48 bytes for the whole matrix'''
It would be stored in the order of the rows and go from the first element of the array to the last within those rows. Ex: a[0][1] = 00000000|00000000|00000000|00000001
(Edited: 2021-11-03)It would allocate 48 bytes of memory for this array. 3 * 4 * 4 The way that the values would be written in memory is one row after another. For example: {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}