File: safeToReturn.cpp

package info (click to toggle)
blitz%2B%2B 1%3A1.0.2%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 8,568 kB
  • sloc: cpp: 57,803; python: 1,941; fortran: 1,510; f90: 852; makefile: 833; sh: 321
file content (47 lines) | stat: -rw-r--r-- 1,122 bytes parent folder | download | duplicates (3)
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
#include "testsuite.h"
#include <blitz/array.h>

using namespace blitz;

const int N=1000;

// To return an expression that references local arrays, the
// expression has to be wrapped in safeToReturn(), which turns
// FastArrayIterators into FastArrayCopyIterators which keep a copy of
// the arrays instead of referencing them.

// this is safe
BzBinaryExprResult<Multiply, Array<double,1>, Array<double,1> >::T_result::T_range_result
good()
{
  Array<double,1> a(N);
  a=tensor::i;
  Array<double,1> b(N);
  b=sin(a);
  return safeToReturn(a*b);
}

// this is unsafe
BzBinaryExprResult<Multiply, Array<double,1>, Array<double,1> >::T_result
bad()
{
  Array<double,1> a(N);
  a=tensor::i;
  Array<double,1> b(N);
  b=sin(a);
  return a*b;
}

int main()
{
  // switch to expr=bad() and it will crash.
  BzBinaryExprResult<Multiply, Array<double,1>, Array<double,1> >::T_result::T_range_result expr=good();

  //BzBinaryExprResult<Multiply, Array<double,1>, Array<double,1> >::T_result expr=bad();
  Array<double,1> result(N);
  result=expr;
  BZTEST(all(abs(result-tensor::i*sin(1.0*tensor::i))<1e-5));

    return 0;
}