CS1010 Notes
  • Welcome
  • Lec/Tut/Lab/Exes
    • Lecture
      • Lec 01 - Computational Problem Solving
      • Lec 02 - Functions and Types
      • Lec 03 - Basic C Programming
      • Lec 04 - Conditionals
      • Lec 05 - Loops
      • Lec 06 - Call Stacks, Arrays
        • Diagnostic Quiz
      • Lec 07 - Pointers, Memory management
        • Diagnostic Quiz
      • Lec 08 - Multi-d Array, Efficiency
        • Diagnostic Quiz
      • Lec 09 - Searching and Sorting
        • Diagnostic Quiz
      • Lec 10 - More Recursion
        • Diagnostic Quiz
      • Lec 11 - Strcut & Standard I/O
        • Diagnostic Quiz
      • Lec 12 - Recap
    • Tutorial
      • Tut 01 - Computational Problem-Solving
      • Tut 02 - Functions and Conditionals
      • Tut 03 - More on Conditionals
      • Tut 04 - Loops
      • Tut 08 - Searching and Sorting
    • Lab
      • Lab 01 - Unix/Vim Setup
      • Lab 02 - Debugging
      • Lab 03 - Assert
      • Lab 04 - Test Cases
      • Lab 05 - Arrays
      • Lab 06 - Memory Errors
      • Lab 07 - Compiling with Clang
      • Lab 08 - C Preprocessor
      • Lab 09 - Backtracking
      • Lab 10 - Struct and Wrap up
    • Exercises
      • Exercise 3 - Fixed-Length Arrays
      • Exercise 4 - Dynamic Arrays and Strings
      • Exercise 6 - Searching and Sorting
      • Exercise 7 - More Recursion
      • Exercise 8 - Struct
  • Past Year Exam
    • Midterm PE
      • PE1 (AY18/19)
      • PE1 (AY20/21)
      • PE1 (AY21/22)
      • PE0 (AY22/23)
      • PE0 (AY23/24)
    • Midterm Paper
      • Midterm (AY18/19)
      • Midterm (AY20/21)
      • Midterm (AY21/22)
      • Midterm (AY22/23)
    • PE1 Review
      • PE1 (AY23/24)
    • PE2 Review
      • PE2 (AY18/19)
      • PE2 (AY20/21)
      • PE2 (AY21/22)
      • PE2 (AY22/23)
      • PE2 (AY23/24)
    • Final Paper
      • Final (AY18/19)
      • Final (AY20/21)
      • Final (AY21/22)
      • Final (AY22/23)
      • Final (AY23/24)
  • Current Year Exam
    • PE0 (AY24/25)
    • PE1 (AY24/25)
    • PE2 (AY24/25)
    • Final (AY24/25)
  • Toolbox
    • Vim & Unix
    • GDB
  • After CS1010
Powered by GitBook
On this page
  • Exercise 4 Review
  • Dynamic Array
  • Null Pointer Check
  • 7. Kendall
  • 9. Subtract
  • Compiler
  • Multidimensional Arrays
  • Big O
  • Selected Problems from Exercise 5
Edit on GitHub
  1. Lec/Tut/Lab/Exes
  2. Lab

Lab 07 - Compiling with Clang

PreviousLab 06 - Memory ErrorsNextLab 08 - C Preprocessor

Last updated 6 months ago

Slides:

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.

When do should we use malloc()/ dynamic array?

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.

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()!

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

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

This will define the starting point for each row.

  1. 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))f(x) = O(g(x))f(x)=O(g(x)) means f(x) is upper bounded by g(x). or f∈O(g)f \in O(g)f∈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

322KB
Lab7.pdf
pdf
Lab 7 Slides