CS61C
Lecture Notes
61C Lecture Notes:
LEC03: COMPSCI 61C - 2018-08-27
Review lecture 1 on number systems
https://www.youtube.com/watch?v=mX0SKTQY7tg
Many versions of C: K&R C to C99 to C11
Overview of C:
C compiles to architecture specific machine code (as opposed to Java)
Pros:
Good runtime performance (optimized for architecture)
Okay compile time performance
Cons:
Have to port it for different architectures
Intro stuff main:
argv[0] holds the name of the program + arguments ....
argc is the number of strings used in the invocation (command + args inclusive)
Memory:
One long array of cells
All of C is pass by value/copy (this is why we need pointers)
Pointers:
int p*; // Initializing a pointer
p = &some_integer_var;
printf("%d", *p) // Dereferencing the pointer
p* = 7; // some_integer_var will now be assigned to the value of 7
LEC04: COMPSCI 61C - 2018-08-29
Arrays
Very similar to pointers (though not entirely the same)
int list_of_nums[32]; // declares an array of 32 integers
int list_of_nums[2] = {1, 2}; // declares and initializes
list_of_nums++; printf(%u, list_of_nums*); //This will print 2
Declaring an array in a subroutine and returning the address to the array is problematic (Why? Because C assumes you will not mess with memory allocated within a subroutine after it has returned)
Pointer arithmetic
Pointer arithmetic is smart.
int p* = some_integer_array;
p = p + 1; // Add one to the pointer
This will increment the address stored in p to 4 bytes (i.e the next integer in the array)
This works similarly for structs and so on
sizeof
sizeof gives the size in bytes for a given type/variable/array
sizeof(int)// 4 bytes
sizeof(int *) // This should be the size of an unsigned integer (since pointers are of type unsigned integers)
int * p = some_array; sizeof(p); // Same as above
Arrays are the exception to this. Sizeof an array will actually give the size of the array
int array[2] = {1,2}; sizeof(array); // 8
malloc
We can explicitly allocate memory in the following manner
int *p = (int *) malloc(5* sizeof(int));
This is one way of creating an array of 5 integers
Malloc returns a pointer of type void * so we must use a typecast
free(p)
Give free the previously allocated address when you are done using it.
Don't free twice
Don't give it an address malloc didn't give you
Dangling reference - Using an un-initialized pointer
Memory leak - Not freeing memory you have used
Lastly, don't ask for the address of an array. The value held at the array variable is the address of the first entry in the array, but the address of the array variable is unknown.
Questions:
Why use malloc to create arrays when you can just use arrays?
LEC05: COMPSCI 61C - 2018-08-31
Lecture number 1 on memory management.
Structs
Make composite objects composed of simpler types
Syntax:
// Assume there is a struct for a car
struct car *pointer_to_my_car;
(pointer_to_my_car)*.wheels += 1;
OR we can do this
pointer_to_my_car->wheels +=1;
Defining type of a node pointer
typedef Node *List; // Type List is now of type pointer to a struct
List l = //pointer to a node
This is a good slide explaining structs, pointers, and malloc

Address space of a program
Composed of 4 regions of memory
Stack: local vars, arguments, return pointers
Heap: malloc, free
Static Storage: globals,
Code:
Stack grows down from higher addresses to lower addresses
Heap grows up
Why we need to use malloc (instead of arrays)?
Variable declared in a subroutine lives on the stack.
BUT stack gets overwritten by the next frame overwriting that memory
Example:
printf frame overwrites the part of the memory allocated by the ptr function

Heap and Fragmentation
Fragmentation - most memory is in small non-contiguous chunks
How is memory on the heap managed?
Last updated