Docsity

Intermediate-Level Language and Explicit Memory Management | CS 4410, Study notes of Operating Systems

Partial preview of the text

Download Intermediate-Level Language and Explicit Memory Management | CS 4410 and more Study notes Operating Systems in PDF only on Docsity!

C for Java Programmers

CS 414 / CS 415

Niranjan Nagarajan

Department of Computer Science

Cornell University

[email protected]

Original Slides: Alin Dobra

Why use C instead of Java

  • Intermediate-level language: - Low-level features like bit operations - High-level features like complex data-structures
  • Access to all the details of the implementation - Explicit memory management - Explicit error detection
  • Better performance than Java

All this make C a far better choice for system programming.

Hello World Example

/* Hello World program */ #include <stdio.h>

void main(void){ printf("Hello World.\n"); }

$ ./hello Hello World.

Primitive Types

  • Integer types: - char : used to represent characters or one byte data (not 16 bit like in Java) - int,short and long : versions of integer (architecture dependent) - can be signed or unsigned
  • Floating point types: float and double like in Java.
  • No boolean type, int or char used instead. - 0 ⇒ false - 6 = 0 ⇒ true

Arrays and Strings

  • Arrays:

/* declare and allocate space for array A */ int A[10]; for (int i=0; i<10; i++) A[i]=0;

  • Strings: arrays of char terminated by \

char[] name="CS415"; name[4]=’5’;

- Functions to operate on strings in string.h. ∗ strcpy, strcmp, strcat, strstr, strchr.

printf function

  • Syntax: printf(formating_string, param1, ...)
  • Formating string: text to be displayed containing special mark- ers where values of parameters will be filled: - %d for int - %c for char - %f for float - %lf for double - %s for string
  • Example:

printf("The number of students in %s is %d.\n", "CS415", 80);

Pointers

  • address of variable : index of memory location where vari- able is stored (first location).
  • pointer : variable containing address of another variable. type* means pointer to variable of type type.
  • Example:

int i; int* ptr_int; /* ptr_int points to some random location / ptr_int = &i; / ptr_int points to integer i / (ptr_int) = 3; /* variable pointed by ptr_int takes value 3 */

  • & address operator, * dereference operator.
  • Similar to references in Java.

Pointers (cont.)

  • Attention: dereferencing an uninitialized pointer can have arbitrary effects (including program crash).
  • Good programming advice: - if a pointer is not initialized at declaration, initialize it with NULL, the special value for uninitialized pointer - before dereferencing a pointer check if value is NULL int* p = NULL; . . . if (p == NULL){ printf("Cannot dereference pointer p.\n"); exit(1); }

Structures (cont.)

  • Structures can have as elements types already defined.
  • Structures can refer to pointer to themselves:

struct list_elem{ int data; struct list_elem* next; };

  • -> is syntax sugaring for dereference and take element:

struct list_elem le={ 10, NULL }; struct list_elem* ptr_le = ≤ printf("The data is %d\n", ptr_le->data);

Data-type Synonyms

  • Syntax: typedef type alias;
  • Example:

typedef int Bool; Bool bool_var; typedef int* Intptr; Intptr p; /* p is a pointer to int / typedef struct list_el list_el; / list_el is alias for struct list_el / struct list_el { int data; list_el next; /* this is legal */ };

  • Advantage : easier to remember, cleaner code.

Common Syntax with Java

  • Operators: - Arithmetic: ∗ +,-,,/,% ∗ ++,--,=,... - Relational: <,>,<=,>=,==,!= - Logical: &&, ||, !,? : - Bit: &,|,ˆ,!,<<,>>

Common Syntax with Java (cont.)

  • Language constructs: - if( ){ } else { } - while( ){ } - do { } while( ) - for(i=0; i<100; i++){ } - switch( ) { case 0: ... } - break, continue, return
  • No exception handling statements.

Memory Allocation and Deallocation(cont.)

Local variables:

  • Characteristic: are declared in the body of a function.
  • Space allocated when entering the function (function call).
  • Initialization before function starts executing.
  • Space automatically deallocated when function returns: - Attention: referring to a local variable (by means of a pointer for example) after the function returned can have unexpected results.
  • Names have to be unique within the function only.

Memory Allocation and Deallocation(cont.)

Heap variables:

  • Characteristic: memory has to be explicitly: - allocated: void* malloc(int) (similar to new in Java) - deallocated: void free(void*)
  • Memory has to be explicitly deallocated otherwise all the memory in the system can be consumed (no garbage col- lector).
  • Memory has to be deallocated exactly once, strange behav- ior can result otherwise.