Lecture 2026-02-05

Recall:

void inc (int *x){
*x++; //wrong,
// because of precedence ( the postfix always takes precedence over prefix ! ) 

*x = *x + 1;
*x+= 1;
++*x; 
//above three implementations work
}
circle-info

In C, post fix takes precedence over prefix.

So *x++ means *(x++)

Address is incremented, a value is fetched from the unincremented adder and thrown away. No change to the original var.

Correct:

int inc (int *x){
(*x)++;
}

Now consider:

WRONG because `p.x`, are evaluated first, because of post-fix before prefix. There's TWO reasons why it won't compile. 1. It doesn't make sense to star an integer. 2. Pointers do not have an `x` or `y` field and thus won't even compile
void swap(struct Postn *p){ //its going to take in a POINTER
int temp = *p.x = *p.y;
*p.x = *p.y;
*p.y = temp;
}
circle-exclamation
circle-info

Make sure its clear when to use an arrow and when to use a dot.

Full access through ptr happens a lot - shortcut:

  • means (*p).x

More sophisticated user input:

  • Reads x as a decimal integer.

    • Skips leading white step.

  • Why is this wrong?

    • scanf is a function - can't modify x

thus, instead:

  • scanf returns the number of arguments actually read.

    • A lot of options and variety to modify scanf —its very complicated.

Advanced Mutation

  • Mutating structures + lists

    • In Scheme

      • can mutate parts of cons of a cons with

        • set-car!

        • set-cdr!

    • In Racket

      • cons fields are immutable

        • cannot be mutated.

  • For mutable pairs, Racket has mcons

    • Mutating fields with:

      • mset-car!

      • mset-cdr!

For structs: provide the option #:mutable to allow mutation.

Semantics:

  • 1A - (posn v1 v2) is a value

  • NOW- (posn v1 v2) cannot be a simple value if its mutable

    • has to be behave more like a BOX

      • since its a list of elts

How?

  • is a struct automatically boxed?

  • or is the struct a box?

    • SOL: CHECK IN RACKET

circle-info

A struct is not automatically boxed, but it does box its contents !

  • So we can rewrite (pos v1 v2) as:

    • (define _val1 v1)

    • (define _val2 v2)

    • (posn _val1 _val2)

circle-info

Recall what underscore means ( won't be expanded ).

  • where p is (posn _val _val2)

    • find the definition for _val , fetch thev alue

  • where p is (posn _val1 _val2)

    • find (define _val1 ....)

    • replace with

      • (define _val1 v)

THINK BOXES!!

  • generalizes to any mutable structs, mcons

Consider:

  • Without underscore variables, this produce 1, but in fact its 4.

The two (box 1)s are the same box , can't observe this without the _ vars.

  • under our new ruels

  • Note how the shared item is now reflected and explictly shown !

But its not just the box that is shared.

  • The entire tail is shared since lst1 is shared!

    • Not being duplicated but memory and their associated values are being shared.

      • This can be seen since using cons is O(1)

    Both occurrences of lst1 refer to the same lst1.

  • So we need to rethink define:

circle-info

Thus, it is NOT just a replacement of all x's with 3, like we initially treated in 1A, otherwise we would have gotten 3.

  • So x is not just a value

    • Its something we can mutate

    • an entity we can access

Thus, x must denote a LOCATION and the location contains the value.

  • So we don't have just one lookup δ\delta : var -> value

    • Thus we have two lookups:

      • var -> location

      • location -> value

Thus, set! changes the lcoation -> map, but not the var->location map (nothign changes that )

circle-info

This is generally true, but only in the last two weeks we will discuss the complexities in racket and for why its not true.

(define ... ) creates a location, fills it with a value.

In C:

Consider

Outputs: 3 3 3

Why?

  • y initialized to x's address

  • y points to the location where x resides

  • z initalized equal to y

    • Therefore, z is also to x address

  • *y=2 stores 2 at x's location x==*y==*z==2

  • *z=3 stores 3 at x's location x==*y ==*z ==3

Drawing

  • x,*y,*z

    • three different names for the same data

  • Called aliasing

    • accessing the same data by different names.

Last updated