Sei sulla pagina 1di 4

CS 354 - Machine Organization

Wednesday, September 14, 2016


We assume that you have successfully completed CS 302 and CS/ECE 252.
Jim Skrentny, 5379 CS, skrentny@cs.wisc.edu
Canvas website: https://canvas.wisc.edu/courses/3949
Outlines: On Canvas - select Files and look in the outlines folder.
Waitlisted? Continue attending. Some seats open as students finalize their schedules.
Homework hw1 (1.5%) due 10 pm this Friday, September 16th
Project p1 (3%) 10 pm Monday, September 19th
Exam conflicts: report any on Piazza by Wednesday, September 21st (see post)
Last Time
Pointer Basics * and &
1D Arrays and Pointers
Today
2D Arrays and Pointers to Pointers (from last time)
1D Arrays on the Heap
2D Arrays on the Heap
Pointer Caveats
Next Time
Read: K&R Ch. 6
Passing Pointers to Functions
Structs and Pointers

Copyright 2016 Jim Skrentny

CS 354 (F16): L4 - 1

1D Arrays on the Heap


What?

Why?

How?
malloc(size_in_bytes):

sizeof(operand):

 Write the code that makes an integer array named a having 5 elements (dynamic alloc).

 Draw a memory diagram showing array a.

 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;

Copyright 2016 Jim Skrentny

CS 354 (F16): L4 - 2

2D Arrays on the Heap


Java Array of Arrays
int[][] m = new int[ROWS][COLS];

 Draw a memory diagram showing the Java array m (assume ROWS is 2, COLS is 4).

Allocating an Array of Arrays in C


How do we allocate a 2D array on the heap like Java?
 Write the code that declares a pointer to an integer pointer named m.

 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):

Copyright 2016 Jim Skrentny

CS 354 (F16): L4 - 3

Pointer Caveats


Don't dereference freed pointers!


int *p = malloc(sizeof(int));
int *q = p;
. . .
free(p);
p = NULL;
. . .
*q = 11; //very confused face!

dangling pointer:

Watch out for memory leaks!


int
int
. .
p =

*p = malloc(sizeof(int));
*q = malloc(sizeof(int));
.
q; //imprudent face!

Don't dereference uninitialized pointers!


int *p;
*p = 11; //frown and sometimes confused face!

Don't dereference NULL pointers!


int *p = NULL;
*p = 11; //frown face!

Be careful with testing for equality!


compares nothing because its assignment
compares addresses in pointers
compares values in pointees

And one more thing:




Arrays have no bounds checking!


int a[5];
for (int i = 0; i < 11; i++)
a[i] = 0; //frownfusald face

Copyright 2016 Jim Skrentny

CS 354 (F16): L4 - 4

Potrebbero piacerti anche