Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
CS 354 (F16): L4 - 1
Why?
How?
malloc(size_in_bytes):
sizeof(operand):
Write the code that makes an integer array named a having 5 elements (dynamic alloc).
Write the code that gives the element at indexes 0, 1 and 2 a values of 0, 11 and 22
by using pointer dereferencing, indexing, and address arithmetic respectively.
Write the code that uses p to give the element at index 3 a value of 33.
int *p;
CS 354 (F16): L4 - 2
Draw a memory diagram showing the Java array m (assume ROWS is 2, COLS is 4).
Write the code that assigned m an allocation of an array of integer pointers of size ROWS.
Write the code that assigns each element of the pointer array (each row)
an allocation of an array of integers of size COLS.
What is the contents of m after this code executes (assume ROWS is 2 and COLS is 4)?
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++)
*(*(m+i)+j) = i + j;
Will the following replacement for the underlined code above produce the same result?
*(*m + COLS*i + j)
What happens to the memory when youre done with it?
free(pointer):
CS 354 (F16): L4 - 3
Pointer Caveats
dangling pointer:
*p = malloc(sizeof(int));
*q = malloc(sizeof(int));
.
q; //imprudent face!
CS 354 (F16): L4 - 4