Grabbag
Dynamic Memory
Compilation
ADTs
100

What is buffering? How does the operating system do this?

A buffer is memory that stores a chunk of a file for reads and writes.

The OS buffers files into blocks, which are the smallest unit of the file that is read at a time.

100

What are the four main memory allocation functions, and what do they do?

malloc - allocates memory

calloc - allocates memory and clears it

realloc - reallocates memory

free - frees memory back to the OS

100

What are the steps of compilation (the entire process) in order?

(hint: there are five of them)

preprocessor, compiler, assembler, linker, loader,

100

To create an ADT, you need to use a ______ to define a name that is ____ to the client.

typedef, opaque

200

What is a system call? What is an example of a system call?

A system call is a function call made to the operating system.

Some examples are read, write (https://www.man7.org/linux/man-pages/man2/syscalls.2.html)

200

Allocate enough space for 30 elements for the variable x:

int * x;

x = (int*)malloc(sizeof(int) * 30);

200

Write a C-preprocessor macro which will give you sum of three numbers.

#define SUM(x,y,z) (x + y + z)

200

Define the user-accessible type for an ADT. It should be anonymous.

#ifnddef STACK_IMPLEMENTATION

typedef struct {} * ADT;

#endif

300

Write a C struct to define a binary tree, tree_node_s. A given node must store a string.

struct tree_node_s {

  char * data;

  struct tree_node_s * left;

  struct tree_node_s * right;

};

300

Write a short code snippet that leaks 100 bytes of memory

int * x = malloc(100);

int * x = 0;


300

Place the following steps in the correct order: compiler, assembler, preprocessor, loader, linker. Explain briefly what occurs in each step.

Preprocessor: text replacement
Compiler: C code -> assembly code
Assembler: assembly code -> machine language code (object file)
Linker: object files -> executable
Loader: loads executable into memory

300

Explain the difference between Static and Dynamic typing, and give examples of languages that use them.

Static: checks types at compile time; C
Dynamic: checks types during execution, Python

400

What is the line of code to open a file called "test.txt" in order to read it?

FILE * file = fopen( "test.txt", "r" );

400

Write a code snippet that creates a dangling pointer.

int *x = malloc(sizeof(int) * 20);

int *y = x;

free(y);

// x is now a dangling pointer!

400

Define a macro that takes in a parameter (assumed to be a float) and prints both the name and value of the variable.

#define PRT(x) printf( #x “: %f\n”, x )

400

Explain the difference between a strongly and weakly typed language. Which category is C?

Strongly typed: can’t get around type restrictions
Weakly typed: can get around type restrictions


C is weakly typed (you can get around restrictions, ex: adding a pointer to an integer, casting, etc.)

500

What is the output of the code?

(1.png)

x1: 11
x2: 16


500

Draw a memory diagram for the following code. Separate the stack and heap.

int main() {
   
    int a = 8;
    int *a_ptr = malloc(sizeof(int));
   
    int b = 10;
    int *b_ptr = &b;

    int **b_ptr_ptr = &b_ptr;

    *a_ptr = 5;

    return 0;
}


(img 2)

500

Define the following parts of just the compiler, and write what each produces.

Lexical analysis
Syntax analysis
Semantic analysis
Intermediate code generator

Lexical analysis: Breaks code into tokens
Syntax analysis: Creates a syntax tree from tokens
Semantic analysis: Checks correctness of syntax tree
Intermediate code generator: generates intermediate representation

500

Given the following struct used in a stack abstract data type, write a function, push_element, which adds an element to the top of the stack. If the stack is full, it should allocate more memory.

struct stack {
  float * data;
  long size;
  long max_size;
}
typedef struct stack * StackADT;

void push_element( StackADT stack, float * num ) {
  if( stack->size == stack->max_size ) {
    float * temp = (float *) realloc( stack->data, stack->size * 2 );
    assert( temp != NULL );
    stack->data = temp;
    stack->max_size *= 2;
  }
  stack->data[stack->size] = num;
  stack->size++;
}