Abstractions and Programming

6 September 2005

The Really Big Ideas

Just Bits

Data Types

Memory

Memory

It's an ABSTRACTION!

GP Registers

Just enough C

For our purposes C is almost identical to JAVA except:

C pointers

int i;     // simple integer variable
int a[10]; // array of integers
int *p;    // pointer to integer(s)

*(expression) is content of address computed by expression.

a[k] == *(a+k)

a is a constant of type “int *”

a[k] = a[k+1] is equivalent to
*(a+k) = *(a+k+1)

Legal uses of C Pointers

int i;       // simple integer variable
int a[10];   // array of integers
int *p;      // pointer to integer(s)

p = &i;      // & means address of
p = a;       // no need for & on a
p = &a[5];   // address of 6th element of a
*p           // value of location pointed by p
*p = 1;      // change value of that location
*(p+1) = 1;  // change value of next location
p[1] = 1;    // exactly the same as above
p++;         // step pointer to the next element

Legal uses of Pointers

int i;     // simple integer variable
int a[10]; // array of integers
int *p;    // pointer to integer(s) 

So what happens when

p = &i;

What is value of p[0]? What is value of p[1]?

C Pointers vs. Object Size

Does p++ really add 1 to the pointer?

char *q; 
...
q++; // really does add 1

Clear123

void clear1(int array[], int size) {
        for(int i=0; i<size; i++)
                array[i] = 0;
}

void clear2(int *array, int size) {
        for(int *p = &array[0]; p < &array[size]; p++)
                *p = 0;
}

void clear3(int *array, int size) {
        int *arrayend = array + size;
        while(array < arrayend) *array++ = 0;
}

Pointer Summary

In the “C” world and in the “machine” world:

More details:

C vs. ASM

Overview_and_Big_Ideas?action=AttachFile&do=get&target=CtoASM

Form of the Instructions

add $t0, $a0, $t0

Naming Registers

This is all just software convention

What are the operands?

C versus ASM

Overview_and_Big_Ideas?action=AttachFile&do=get&target=CtoASM

Cultural Highlights

last edited 2005-09-06 17:42:34 by GaryBishop