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
  • Problems
  • 3. String Literal
  • 9. Illegal Memory Access
  • 12. String Literal
  • 13. Time Complexity
Edit on GitHub
  1. Past Year Exam
  2. Final Paper

Final (AY20/21)

PreviousFinal (AY18/19)NextFinal (AY21/22)

Last updated 6 months ago

Problems

Question paper without answers:

Question paper with answers

3. String Literal

String Literal is stored in a read-only memory location, which is not stack!!!

9. Illegal Memory Access

In this problem, we can summarise the following four cases when illegal memory access happens:

  1. When you return an address on the stack that has been "cleared"

long *bar()
{
    long x = 10;
    return &x;
}
  1. When you try to write to (access) the address stored in an unintialized pointer.

long *bar()
{
    long *px;
    *px = 10;
    return px;
}
  1. When you did not allocate enough memory on the heap for the variable that a pointer points to. And you try to "access" the pointer by trying to write value into it.

long *bar()
{
    long *px;
    px = (long *)malloc(1);
    *px = 10;
    return px;
}
  1. When you access some address like decimal number 10, etc

long *bar()
{
    return (long *)10;
}

12. String Literal

According to CS1010 Notes,

A string literal refers to a string written between two " characters, such as "Hello world!". And it is stored in a read-only memory region (not the stack).

// Illegal
char *str1 = "Hello!";
str1[5] = '.';

// Legal
char str2[7] = "Hello!";
// or char str2[] = "Hello!"
str2[5] = '.';

The difference between the two is that str1 points to a read-only region in the memory, while str2 contains a copy of the string on the stack.

From this question, we can see that to create a copy of the string literal on the stack using arrays, we have two methods:

  1. char str[] or

  2. char str[num], where num is an integer number

And it is only when we define a pointer that points to the read-only memory region can't we modify its content.

13. Time Complexity

The (f) part is similar to AY22/23 Q14. Can compare and study later.

For now, I would say one possible way to reason is to see how many swap times we need to move the correct number to the correct position. For example, if our input is 4321, to move 4 to the last, in this algorithm, we need 4 swaps. (Although it is not the case you move 4 all the way to the last like what we have seen in the bubble sort algorithm, the actual case is when you move 4 to a "temp" place, you will move 3 to a "temp" place also, which may be a little bit consuing. But anyway, the total swap times we need is 4 for the number 4). Similarly, for 3, we need 3 moves. So, for the input n, we just need the sum from 1 to n, which is O(N2)O(N^2)O(N2).

85KB
final-2021-s1.pdf
pdf
Question without Answers
106KB
final-2021-s1-with-comments.pdf
pdf
Question with Answers