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
  • Pointers
  • Type Match
  • Pointer Arithmetic
  • Heap
  • size_t
  • String
  • String Literals
Edit on GitHub
  1. Lec/Tut/Lab/Exes
  2. Lecture

Lec 07 - Pointers, Memory management

PreviousDiagnostic QuizNextDiagnostic Quiz

Last updated 6 months ago

Slides:

  1. Use the "black box" idea to re-think about pointers.

  2. Two golden rules

  3. malloc and calloc the idea of "borrow" and heap, refer back to Eldon's explaining with size_t.

  4. The use of size_t, refer back to eldon's explanation

  5. String Literal is stored in another section of memory which you won't have access.

Pointers

Type Match

When we store a memory address to a pointer, the types must match. Let's consider:

long radius = 5;
double *addr;
addr = &radius; // not ok

Line 3 above would lead to a compilation error since we try to point a double pointer to a long.

One exception to this rule is the type void *. A pointer to void has a special place in C, and it can be used to point to any other type without type errors.

The & operator cannot be used on the left-hand side of the assignment operation. For instance

long x = 1;
long y = 2;
&x = &y; // error

Pointer Arithmetic

We can perform arithmetic operations on pointers, but not in the way you expect. Suppose we have a pointer:

long x;
long *ptr;
x = 1;
ptr = &x;
ptr += 1;

Suppose that x is stored in memory address 1000, after Line 4, ptr would have the value of 1000. After the line ptr += 1, using normal arithmetic operation, we would think that ptr will have a value of 1001. However, the semantics for arithmetic operations differ for pointers. The + operation for ptr causes the ptr variable to move forward by the size of the variable pointed to by the pointer. In this example, ptr points to long, assuming that long is 8 bytes, after ptr += 1, ptr will have the value of 1008.

We can only do addition and subtraction for pointers.

Heap

size_t

size_t is just a fancy way of saying non-negative integer.

While both size_t and long are integer types, they are not compatible with each other. Explicit casting is needed to assign the value of one type to the other.

It is also a common bug to write code like this:

size_t i = 100;
while (i >= 0) {
    // do something to a[i]
    i -= 1;
}

which loops forever, since i >= 0 is always true.

String

String Literals

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 common between str1 and str2 is that both of themselves are on the stack. The difference between the two is that str1 points to a read-only region in the memory (but str1 itself is a pointer on the stack), while str2 contains a copy of the string on the stack.

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.

CS1010 AY24/25 S1 Lecture 7
Lecture Slides