CS 70

Arrays

Setup

Form a group of 3–4 people, and once you've formed your group, ask Prof. Melissa for a group number. Someone from the group should log into the CS 70 server on one of the lab computers.

If There's a Power Outage

You should have already installed a working C++ clang compiler on your own machine, so just copy the code below into arrays.cpp on a laptop and share it for the group work.

Also, if you have a Mac, you may also want to try running the code in OnlineGDB as it'll behave more like the CS 70 server than your own machine will.

If the Power has Come Back

Run:

cd /cs70/fall2026/lab/week03lab1/groupNN

where NN is your group number (e.g., group03 if you're group 3).

You'll find this code in arrays.cpp. You can see it here, or edit it with Helix (hx arrays.cpp).

#include <iostream>
#include <cstddef>

int main() {
    constexpr size_t ARRAY_SIZE = 10;
    int x = 42;
    int numbers[ARRAY_SIZE];
    int y = 54;

    // Initialize the array
    for (size_t i = 0; i < ARRAY_SIZE; ++i) {
        numbers[i] = int(i) + 100;
    }

    // Print the array itself
    std::cout << numbers << std::endl;

    // Print the array elements
    for (size_t j = 0; j < ARRAY_SIZE; ++j) {
        std::cout << "Element at index " << j << " : "
                  << numbers[j] << std::endl;
    }

    // Print x and y to show they are unchanged
    std::cout << "x == " << x << ", y == " << y << std::endl;

    return 0;
}

Steps

Step 1, Reading the Code

Read over the code and decide what you think it will do. Discuss with your group and make a prediction about the output.

Step 2, Compiling and Running the Program

Compile the program by running:

clang++ -std=c++17 -Wall -Wextra -pedantic -c arrays.cpp

then, if that succeeeds, make an executable by running the linker command:

clang++ -o arrays arrays.o

and finally (assuming that step succeeded), run the program by running:

./arrays

You can also run all the steps at once, stopping if something fails, by running:

clang++ -std=c++17 -Wall -Wextra -pedantic -c arrays.cpp && \
clang++ -o arrays arrays.o && \
./arrays

Look at the output. In particular, remember the values of x and y for later.

Step 3, Changing Array Access to Use the * Operator

Change the array access to use the * operator instead of []. Remember that the form a[i] is equivalent to *(a + i). You can just change the part that initializes the array if you like. Compile, link and run it to check it works the same.

Step 4, A Silly Way to Index an Array

Change the array indexing to be i[numbers] instead of numbers[i]. Before you compile, speculate about whether this will compile, and if so, whether it will work the same and why. Compile, link and run to find out.

Step 5, Overstepping the Bounds of the Array

Change the first for loop to be < ARRAY_SIZE+4, overstepping the bounds of the array. Why is this "illegal" in C++'s world? What results do you get? Before you run it, speculate about what will happen with your partner(s). Compile, link and run it.

What happened? (Hint: Look at the values of x and y that you printed out earlier and contrast them with the values you see now.)

What is so bad about this? What is good about it?

Leave the code so that it keeps overstepping the bounds of the array for now; we'll do more investigation of this in later steps.

Step 6, Changing the Type of x and y

Change x and y to be float instead of int. Before you run it, speculate about what will happen with your partner(s). Compile, link and run it. Did this change anything?

Step 7, Running the Program with Valgrind

Run the program with Valgrind to check for memory errors like this:

valgrind ./arrays

What does Valgrind tell you? Did this help?

Step 8, Using the Sanitizer

Rerun the clang commands adding -fsanitize=undefined,address,bounds to both clang++ commands, like this:

clang++ -fsanitize=undefined,address,bounds -std=c++17 -Wall -Wextra -pedantic -c arrays.cpp && \
clang++ -fsanitize=undefined,address,bounds -o arrays arrays.o && \
./arrays

Step 9, Using a Different Compiler

Try changing the compiler to g++ instead of clang++, like this:

g++ -std=c++17 -Wall -Wextra -pedantic -c arrays.cpp && \
g++ -o arrays arrays.o && \
./arrays

How are the results different from what you saw with clang++? (You could try adding the same -fsanitize=undefined,address,bounds to the g++ commands, but it'll be pretty similar to clang++.)

Step 10, Discussion

Discuss with your group: What does this whole exercise tell you about:

  • How C++ lays out arrays in memory?
    • Did Clang and GCC lay things out the same way?
    • Were they identical to how we draw things in a CS 70 memory diagram?
  • How C++ handles array bounds by default?
  • How C++ is allowed to handle array-bounds violations?
  • How C++ handles undefined behavior in practice?
    • What are the pros and cons of this approach?

Call Prof. Melissa (or a grutor) over when you're done to share your conclusions and discuss any questions you have.

(When logged in, completion status appears here.)