Programming in C++

C-Arrays

Gerald Senarclens de Grancy

What is a C-Array?

A reference to the beginning of consecutive memory of a given type

int numbers[] = {32, 45, 12, 39, 7, 15};
Index 0 1 2 3 4 5
Value 32 45 12 39 7 15
Address 0x7fc8c013e0 0x7fc8c013e4 0x...e8 0x...ec 0x...f0 0x...f4

The index is the offset of the referenced memory.

The first element has index 0.

0 means that there is no offset from the beginning of the referenced memory.

Purpose of C-Arrays

  • Arrays allow storing many elements of the same type
  • These elements can be passed to a function together
  • The number of elements does not have to be known upfront (only C99)

Which Problems do C-Arrays Cause?

  • Arrays only store the memory address of their beginning
  • Passing arrays to functions requires passing their size as well
  • The memory address an array variable references cannot be changed
  • Not all C++ compilers support dynamically sized arrays on the stack
  • Arrays cannot be directly returned from functions

For most C++ programmers, C-arrays only have theoretical value for understanding how vector works. In practice, std::vector should be used almost always instead of C-arrays.

Array Subscription

Subscription is the selection of an element using its index.

int points[] = {32, 45, 12, 39, 7, 15};
int value = points[2];  // access third element with the subscript operator
points[0] = 50;  // write to first element with the subscript operator

Working with C-Arrays

Creating an Array of Constant Size

#include <iostream>

int main() {
  unsigned int numbers[3];  // number of elements known at compile time
  numbers[0] = 15;
  numbers[2] = 5;
  numbers[1] = 10;
  std::cout << numbers[0] << " " << numbers[1] << " " << numbers[2] << "\n";
  return 0;
}

Visualize execution

Determine the Number of Elements

The compiler can determine the number of elements if the array was declared in the current scope.

The sizeof operator returns the total memory in bytes.

#include <iostream>
using std::cout, std::endl;
int main() {
  int values[] = {-5, 39, 13, -21};  // compiler determines number of elements
  cout << "used memory: " << sizeof(values) << endl;
  cout << "number of elements: " << sizeof(values) / sizeof(int) << endl;
  return 0;
}

Visualize execution

Pass the Number of Elements When Passing an Array to a Function

#include <cstdio>

void print_info(int values[], size_t count) {  // values is actually a pointer
  // sizeof returns the size of the pointer (64 bits on modern systems)
  printf("sizeof(values): %ld\n", sizeof(values));
  printf("sizeof(values) / sizeof(int): %ld\n", sizeof(values) / sizeof(int));
  // only way to know the number of elements is a separate argument
  printf("number of elements: %ld\n", count);
}

int main() {
  int values[] = {-5, 39, 13, -21};  // compiler determines number of elements
  printf("sizeof(values): %ld\n", sizeof(values));
  printf("sizeof(values) / sizeof(int): %ld\n", sizeof(values) / sizeof(int));
  print_info(values, sizeof(values) / sizeof(int));
  return 0;
}

Visualize execution

Arrays Are Not Assignable

#include <cstdio>

int main() {
  int first[] = {1, 2, 3};
  int second[] = {4, 5, 6};
  first = second;  // compiler error: array type is not assignable
  first = {7, 8, 9};  // also a compiler error
  printf("{ %d, %d, %d }", first[0], first[1], first[2]);
  return 0;
}
Download not_assignable.cpp

Returning an Array from a Function

It is not possible to directly return an array from a function.

Its memory would be on the stack and the returned reference invalid.

There are three possible approaches

  • Workaround: pass array as argument (use caller's stack memory)
  • Workaround: create static array (preserve memory between calls)
    (requires pointers)
  • Solution: dynamic memory management
    (requires pointers and the free store)

Pass Array as an Argument

#include <cstdio>
#include <ctime>
#include <cstdlib>

void read_values(int values[], size_t count) {
  srand(time(NULL));  // Initialize random number generator
  for (size_t i = 0; i < count; ++i) {
    values[i] = rand();  // assume the values are read from user input
  }
}

int main() {
  int values[3];  // number of elements known at compile time
  read_values(values, 3);
  printf("{ %d, %d, %d }\n", values[0], values[1], values[2]);
  return 0;
}

Visualize execution

Questions
and feedback...