> For the complete documentation index, see [llms.txt](https://wenbo-notes.gitbook.io/cs1010-notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://wenbo-notes.gitbook.io/cs1010-notes/lec-tut-lab-exes/lab/lab-07-compiling-with-clang.md).

# Lab 07 - Compiling with Clang

Slides:

{% file src="/files/TQVSxMq7s8MyVuP9uc8B" %}
Lab 7 Slides
{% endfile %}

## Exercise 4 Review

### Dynamic Array

1. `malloc()` - memory allocation
2. `calloc(n, m)` - calculate the allocation. And it means allocate a memory space for n values, each of size m.

<details>

<summary>When do should we use <code>malloc()</code>/ dynamic array?</summary>

When we don't know the size of the array at the time we run the program. If we know the size of array, use a static one like `long a[10]` is enough.

</details>

{% hint style="info" %}
Note that `calloc()` will "clear the memory" with 0 before it returns the pointer back. So that is why we are **recommended to use `calloc()`!**
{% endhint %}

### Null Pointer Check

> If you don't do Null Pointer Check, you will be penalised in PE

### 7. Kendall

Use math to simplify the algorithm

```
For each i in permutation:
    for each j before i:
        if j > i:
            count += 1
```

### 9. Subtract

An idea to ignore the leading zero is to use start print from the first nonzero element if the result is not 0.

## Compiler

1. `clang -I`, `-I` stands for "include"

## Multidimensional Arrays

1. Remember the way to allocate a 2-D array (with null pointer check)
2. If the contiguous memory method, the code below

```c
for (long i = 1; i < m; i += 1)
{
    matrix[i] = matrix[i - 1] + n;
}
```

This will define the starting point for each row.

3. Due to array decay, the address of an array is the same as the address of its first element.

## Big O

1. $$f(x) = O(g(x))$$ means f(x) is upper bounded by g(x). or $$f \in O(g)$$, which means set of all functions which do not grow more slowly than f.
2. Include the trivial results about Big O calculation in the cheatsheet.
3. If you have two inputs `m` and `n`, you should include both variables because they are independent.
4. The real way to compare growth rate is to take the limit.

## Selected Problems from Exercise 5


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://wenbo-notes.gitbook.io/cs1010-notes/lec-tut-lab-exes/lab/lab-07-compiling-with-clang.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
