> For the complete documentation index, see [llms.txt](https://furkhan324.gitbook.io/workspace/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://furkhan324.gitbook.io/workspace/berkeley/sp-20/cs61c.md).

# CS61C

## 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

{% embed url="<https://www.youtube.com/watch?v=4ED-NdJ9Oek>" %}

* **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&#x20;
* **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

{% embed url="<https://www.youtube.com/watch?v=LsSG4Z2v2Zo>" %}

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

![](/files/-LxXolEQTqAGeXgTse_l)

* **Address space of a program**
  * Composed of 4 regions of memory
    * Stack: local vars, arguments, return pointers
    * Heap: malloc, free
    * Static Storage: globals,&#x20;
    * Code:&#x20;
  * 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

![](/files/-LxXtJiN8CVKUWJogsks)

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