retlocal.cpp

(plain text)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/**
 * Returning data from functions
 * by Shane Beasley <sbeasley@acm.org>
 **/
#include <iostream>
#include <memory> // std::auto_ptr
#include <string> // std::string
#include <vector> // std::vector

/**
 * Pass-by-value function parameters and function-local variables are destroyed
 * when the function returns. Never return pointers or references to them.
 * Instead, try one of the following techniques:
 **/

/** RETURNING SINGLE OBJECTS **/

// return an object by value
int product (int x, int y) {
  int result = x * y;
  return result;
}

// use an "out parameter," data created by the caller to hold the result
void product (int &result, int x, int y) { result = x * y; }
void product (int *result, int x, int y) { *result = x * y; }

// create a new object, store the result in it, and return a pointer to it
// (caller must remember to delete the object when done)
int *new_product (int x, int y) {
  int *result = new int;
  *result = x * y;
  return result;
}

// better: use a "smart pointer" to destroy the result automatically
std::auto_ptr<int> smart_product (int x, int y) {
  std::auto_ptr<int> result(new int);
  *result = x * y;
  return result;
}

/** RETURNING ARRAYS **/

/**
 * You cannot return arrays from functions. However, you can pass in arrays and
 * store results in them, and you can return objects which contain arrays. These
 * techniques are used below.
 **/

// modify an array in situ
void array_product (int array[], int n, int factor) {
  for (int i = 0; i < n; ++i) array[i] *= factor;
}

// use an "out parameter" (described above)
void array_product (int result[], const int array[], int n, int factor) {
  for (int i = 0; i < n; ++i) result[i] = array[i] * factor;
}

// return an object containing an array
struct Array { int values[10]; };
Array array_product (const int array[], int n, int factor) {
  assert(n < 10);
  Array result;
  for (int i = 0; i < n; ++i) result.values[i] = array[i] * factor;
  return result;
}

// return a dynamically-allocated array (caller must delete[] it)
int *new_array_product (const int array[], int n, int factor) {
  int *result = new int[n];
  for (int i = 0; i < n; ++i) result[i] = array[i] * factor;
  return result;
}

// return a dynamically-allocated array (caller must delete[] it)
std::vector<int> vec_array_product (const int array[], int n, int factor) {
  std::vector<int> result(n);
  for (int i = 0; i < n; ++i) result[i] = array[i] * factor;
  return result;
}

int main () {
  using std::cout;
  using std::endl;

  cout << product(2, 1) << endl;

  {
    int n;

    product(n, 2, 2);
    cout << n << endl;

    product(&n, 2, 3);
    cout << n << endl;
  }

  {
    int *p = new_product(2, 4);
    cout << *p << endl;
    delete p;
  }

  cout << *smart_product(2, 5) << endl;

  int array[] = { 6, 7, 8, 9, 10 };
  enum { num_elements = sizeof(array) / sizeof(array[0]) };

  array_product(array, num_elements, 2);

  const int array2[num_elements] = { 11, 12, 13, 14, 15 };
  array_product(array, array2, num_elements, 2);
}