CS 70

Identifying Operations

As we've seen, sometimes it's useful to think about all the operations that occur in a piece of code. For example, if we say

double x = 3 * 4 + 5.0;

there are four operations:

  1. Multiplication: operator*(int,int)
  2. Type conversion: intdouble
  3. Addition: operator+(double,double)
  4. Copy initialization: using double's copy constructor to initialize x

Let's do some more operation spotting…

Loop #1

Suppose we have an array,

double values[MAX_SIZE];

and we want to think carefully about everything that happens in the loop

double* past_end = values+MAX_SIZE;
for (double* p = value; p != past_end; ++p) {
    std::cout << *p << "\n";
}

Make a list of all the operations (i.e., use of a function, constructor/initialization, or operator) that you can see in this code.

In the for loop itself, we can see

  • Copy initialization of a double* (for past_end and p)
  • != operator on two double*s
  • ++ operator to update a variable holding a double*
  • << operator on an ostream and a double value
  • * operator on a double*
  • << operator on an ostream and "\n"

Just before the for loop, we figure out the past-the-end point, so we have

  • + operator on a double* and a ptrdiff_t (like a size_t but signed)

But we'll focus on the for-loop operations.

  • Horse speaking

    Hay! Why did you list << twice?

  • LHS Cow speaking

    Because it's actually two different functions that happen to have the same name. One is the output operator for doubles, and the other is an output operator that takes a C-style string.

Loop #2

Suppose we have the array

double values[MAX_SIZE];

and we want to consider the performance of the loop

for (size_t i = 0; i != MAX_SIZE; ++i) {
    std::cout << values[i] << "\n";
}

Make a list of all the operations (i.e., use of a function, constructor/initialization, or operator) that you can see in this code.

We can see

  • Copy-initialization of a size_t (for i)
  • != operator on two size_t values
  • ++ operator to update a variable holding a size_t
  • << operator on an ostream and a double value
  • [] operator on an array of doubles
  • << operator on an ostream and "\n"
  • Cat speaking

    I converted values[i] into *(values+i) and had two operations for that, * and + on pointers. Is that okay?

  • LHS Cow speaking

    Yes, that's totally fine. It's nice that you remembered that identity, but we don't need to break things down that much right now.

Something to notice here is that the operator[] to go to the ith element of a data structure is a more advanced operator than the ones we used in our previous example. Although arrays can support operator[] efficiently, not all data structures can.

  • Cat speaking

    Like linked lists—they can't.

  • LHS Cow speaking

    Exactly.

  • Duck speaking

    But we're using arrays, so it's fine.

  • LHS Cow speaking

    True. For now. We'll come back to these ideas later.

(When logged in, completion status appears here.)