CSE 3341 Code examples


Rectangle: C

#include<stdlib.h>

#include<stdio.h>

void main(void) {

    typedef struct { double h, w; } Rectangle;

    Rectangle *x, *y;

    x = (Rectangle*) malloc(sizeof(Rectangle)); // todo: check for NULL

    y = x; 

    (*y).h = 3.14;

    y->w   = 2.78;

    printf("Area: %f\n", x->h * x->w);

    printf("x: %p, y: %p\n", x, y);

    free(x); }


Rectangle: Java

class Rectangle {

    double h, w;

    public static void main(String[] args) {

        Rectangle x, y;

        x = new Rectangle();

        y = x;

        y.h = 3.14;

        y.w = 2.78;

        System.out.println("Area: " + x.h * x.w);

        System.out.println("x: " + x + ", y: " + y);  } } 


Unstructured control flow: C

#include <stdio.h>

void main(void) {

    int x = 1, y = 2, z = 3;

    L1: if (x >= 10) goto L3;

    L2: y = y+z;

            if (x == 10) goto L4;

            x = x+1;

           goto L1;

    L3: y = y + 1;

            goto L2;

    L4: printf("x = %d, y = %d", x , y); }


Code segment, static segment, call stack : C

#include <stdio.h>

double x = 1.1; int y = 2;

void f(float a, double b, int c) {

    printf("a: address: %p, size: %d bytes, value: %f\n", &a, sizeof(a), a);

     printf("b: address: %p, size: %d bytes, value: %f\n", &b, sizeof(b), b);

     printf("c: address: %p, size: %d bytes, value: %d\n\n", &c, sizeof(c), c);

    float p = a + 3.14f; double q = b + 2.78; int r = c + 1024;

     printf("p: address: %p, size: %d bytes, value: %f\n", &p, sizeof(p), p);

     printf("q: address: %p, size: %d bytes, value: %f\n", &q, sizeof(q), q);

     printf("r: address: %p, size: %d bytes, value: %d\n\n", &r, sizeof(r), r);

    if (c == 2) f(p, q, r); }

void main(void) {

     printf("address of f: %p\n", &f);     

     printf("address of main: %p\n\n", &main);  

     printf("x: address: %p, size: %d bytes, value: %f\n", &x, sizeof(x), x);

     printf("y: address: %p, size: %d bytes, value: %d\n\n", &y, sizeof(y), y);

    f(3.3f, x, y); }


Java Bytecode

source code

class Rectangle {

    public double height, width;

    public double area() { return height * width; } }

class Main {

    public static void main(String[] args) {

       Rectangle x = new Rectangle();

  x.width = 3.14; } }

bytecode printed with javap -c Rectangle.class Main.class

class Rectangle 

  public double height;

  public double width;

  Rectangle();

          0: aload_0

         1: invokespecial #1   // Method java/lang/Object."<init>":()V

         4: return

  public double area();

         0: aload_0

         1: getfield #7   // Field height:D

         4: aload_0

         5: getfield #13   // Field width:D

         8: dmul

         9: dreturn

class Main 

  Main();

         0: aload_0

         1: invokespecial #1   // Method java/lang/Object."<init>":()V

         4: return

  public static void main(java.lang.String[]);

         0: new #7 // class Rectangle

         3: dup

         4: invokespecial #9 // Method Rectangle."<init>":()V

         7: astore_1

         8: aload_1

         9: ldc2_w #10 // double 3.14d

        12: putfield #12 // Field Rectangle.width:D

        15: return


Static Type Checking in C

#include <stdio.h>

void main(void) {

   double pi = 3.14

   double* dbl_ptr = &pi;

   int* int_ptr = (int*) dbl_ptr;

   int x = *int_ptr;

   printf("x = %d\n");

}