Question

In: Computer Science

Question1 Match the variables with the location and memory layout given the following declarations: int numbers1[5][4];...

Question1
Match the variables with the location and memory layout given the following declarations:
int numbers1[5][4];
int * numbers2 = malloc (5*4*sizeof(int));
int ** numbers3 = malloc (5 * sizeof(int *));
for (int r = 0; r < 5; r++) {
numbers3[r] = malloc(4 * sizeof(int));
}
stack is static (fixed size) memory, heap is dynamic memory (allocated by the program at run time with malloc, calloc, etc)
contiguous memory is memory that is all in one place. All values in a 2D contiguous array are guaranteed to be located in the same place in memory one after another.
numbers1 is stack/ heap contiguous /non-contiguous
numbers2 is stack/ heap contiguous /non-contiguous
numbers3 is stack/ heap contiguous /non-contiguous

Solutions

Expert Solution

int numbrs1 [5][4];

this is a 2d array and it clearly give instruction to declare 20 integers so size is fixed and as this is an array so it allocated contiguous memory

so for numbers1 is stack and contiguous is the right option

int * numbers2 = malloc( 5* 4* sizeof(int));

this is a instruction which tells that a memory of 20 integers to be declared but this is not giving instruction to declare now,

so this get space in heap as this is allocated in dynamic memory and malloc allocates all the size as a complete chunk

so for numbers2 is heap and contiguous is the right option

int **numbers3= malloc( 5* sizeof(int *));

this is telling that we have to allocate a chunk equal to 5* pointer to int

so as this is dynamic allocation so it required heap

now for contiguous and non contiguous see that

numbers3 [r] = malloc( 4* sizeof( int));

so it will allocate a chunk of 4 integer for numbers3[0] , numbers3[1] , numbers3[2] , numbers3[3] , numbers3[4]

but these all statement are different so 5 different chunks of 4 integers are allocated , it can not be guranteed that all chunks get allocated contiguous so

for numbers3 is heap and non-contiguous

I hope this will help you so please give positive ratings :)))


Related Solutions

5. Given the following declarations and assignments, what do these expressions evaluate to? int a1[10] =...
5. Given the following declarations and assignments, what do these expressions evaluate to? int a1[10] = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0}; int *p1, *p2; p1 = a1+3; p2 = &a1[2]; (a) *(a1+4) (b) a1[3] (c) *p1 (d) *(p1+5) (e) p1[-2] (f) *(a1+2) (g) a1[6] (h) *p2 (i) *(p2+3) (j) p2[-1]
Given the following int variables which represent a date, int day; // 0-6 for Sunday-Saturday int...
Given the following int variables which represent a date, int day; // 0-6 for Sunday-Saturday int month; // 1-12 for January-December int date; // 1-31 int year; // like 2020 Define the function void tomorrow(int& day, int& month, int& date, int& year); which updates the variables to represent the next day. Test in main().
Match each function type correctly. Question 1 options: In this parameter type, the memory location of...
Match each function type correctly. Question 1 options: In this parameter type, the memory location of the variables from the invoking function are passed to the called function. In this parameter type, a copy of the variables from the invoking function are passed to the called function. 1. Pass By Value 2. Pass By Reference
The following code segment is stored in memory starting at memory location 0x00445670. What are the...
The following code segment is stored in memory starting at memory location 0x00445670. What are the two possible values for the contents of the PC after the branch instruction has executed?       bgez $a0, skip                      # mem location: 0x00445670 subu $s2, $s1, $t0 # branch NOT taken (false) ori    $v0, $t1, 0x0003 #       skip: addi $t0, $t1, 2 # branch taken (true) if taken: if not taken: Hint: Remember how many bytes each instructions takes.
Determine the memory address of A[3], A[10], A[20] a) given int A[] b) given short A[]...
Determine the memory address of A[3], A[10], A[20] a) given int A[] b) given short A[] c) given char A[]
Given byte-addressed memory with location 0x100 containing 0x62 and location 0x101 containing 0x7C. What is the...
Given byte-addressed memory with location 0x100 containing 0x62 and location 0x101 containing 0x7C. What is the decimal value of the signed short stored at location 0x100: Assuming big-endian addressing. Assuming little-endian addressing. You may assume that a short occupies 2 bytes.
C++ EXERCISES (a) Given int a = 5, b = 2, c = 4, and d...
C++ EXERCISES (a) Given int a = 5, b = 2, c = 4, and d = 5; determine the value of the expression: d % b * c > 5 || c % b * d < 7.    (b) Which repetition statement is preferred for user data input and its validation?              (c) Write a for statement to populate an array, double val[NUMCOUNT], for the following case: Use a counter named double count that has an initial value of 16.2,...
//Question1: What is the name of the following lines in the data type class? private int...
//Question1: What is the name of the following lines in the data type class? private int intVariable; private float floatVariable; private String stringVariable; //Question2: What is the name of the following lines in the data type class? public DataTypeClass_Smith() { intVariable = 0; floatVariable = 0.0; stringVariable = “aString”; } //Question3: What is the name of the following lines in the data type class? public DataTypeClass_Smith( int intVar, float floatVar, String stringVar) { intVariable = intVar; floatVariable = floatVar; stringVariable...
Q1: Given the following code, what is returned by tq(4)? int tq(int num){ if (num ==...
Q1: Given the following code, what is returned by tq(4)? int tq(int num){ if (num == 0) return 0; else if (num > 100) return -1; else     return num + tq( num – 1 ); } Group of answer choices: 0 4 -1 10 Q2: Given that values is of type LLNode<Integer> and references a linked list (non-empty) of Integer objects, what does the following code do if invoked as mystery(values)? int mystery(LLNode<Integer> list) {    if (list.getLink() ==...
Given a memory address of 34Ah (10 bits) with 4 memory banks. Determine the memory bank...
Given a memory address of 34Ah (10 bits) with 4 memory banks. Determine the memory bank address and the address of the word in the bank using Low Order Interleaving (LOI).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT