Topic: Q: Exceptions, vectors, and C code


Author: Helmut Zeisel <zeisel@vai.co.at>
Date: 1997/10/01
Raw View
I have an old C function using callback functions and pure C arrays, say

extern "C"
{
  void f(double a[], int size, CALLBACK cf);
}

The CALLBACK function cf may throw an exception.

1) I know that f does not allocate memory on the heap.
 If f is compiled with a pure C compiler,
 is there any guarantee in the C++ standard
 that the stack of f is cleaned properly
 when cf throws an exception?
 Or is it necessary to recompile f using a C++ compiler?

2) For using f in an exception safe way, I need something like an auto_ptr<double>.
 Such a class for vectors is not provided by Standard C++, so I have to use
 something different.

     a) Will vector<double> do the work?

  CALLBACK cf;
  int n;
  ...
  vector<double> a(n);
  ...
  f(a.begin(), n, cf);


 On my current compiler it works;
 since it will probably not work for a class like
 vector<bool> (which should be optimized for space),
 I get the impression that there is no explicit
 guarantee in the standard that it should work for vector<double>.
 Is this true?

    b)  Will valarray<double> do the work?

  CALLBACK cf;
  int n;
  ...
  valarray<double> a(n);
  ...
  f(&a[0],n,cf);


 From 26.3.2.3, 3: &a[i+j] == &a[i] + j,
 I conclude that it should work.
 Is this true?

Newsfeed unreliable, please answer also by EMail.

Helmut Zeisel <zeisel@vai.co.at>
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: Valentin Bonnard <bonnardv@pratique.fr>
Date: 1997/10/01
Raw View
Helmut Zeisel wrote:
|>
|> I have an old C function using callback functions and pure C arrays,
say
|>
|> extern "C"
|> {
|>   void f(double a[], int size, CALLBACK cf);
|> }
|>
|> The CALLBACK function cf may throw an exception.
|>
|> 1) I know that f does not allocate memory on the heap.
|>         If f is compiled with a pure C compiler,
|>         is there any guarantee in the C++ standard
|>         that the stack of f is cleaned properly
|>         when cf throws an exception?

As I previously said in a similar discution, there is no std
for multi-language compiler (and the statement which says that
extern "C" declare a function callable from C has no normative
meaning).

As previously said in c.l.c.m, not allocating on the heap
isn't equivalent to being exception safe (see Nathan's article).

|>         Or is it necessary to recompile f using a C++ compiler?

It depends, RTM.
Example: old CodeWarrior: recompile, new CodeWarrior: no need to
recompile

|> 2) For using f in an exception safe way, I need something like an
auto_ptr<double>.
|>         Such a class for vectors is not provided by Standard C++, so
I have to use
|>         something different.
|>
|>      a) Will vector<double> do the work?
|>
|>                 CALLBACK cf;
|>                 int n;
|>                 ...
|>                 vector<double> a(n);
|>                 ...
|>                 f(a.begin(), n, cf);

Undefined behaviour, and it may be ill-formed

|>         On my current compiler it works;

amd on your current library implementation (you can switch to
annother STL vendor without chenging your compiler)

|>         since it will probably not work for a class like
|>         vector<bool> (which should be optimized for space),
|>         I get the impression that there is no explicit
|>         guarantee in the standard that it should work for
vector<double>.

Not even any implicit guarantee

|>     b)  Will valarray<double> do the work?
|>
|>                 CALLBACK cf;
|>                 int n;
|>                 ...
|>                 valarray<double> a(n);
|>                 ...
|>                 f(&a[0],n,cf);
|>
|>         From 26.3.2.3, 3: &a[i+j] == &a[i] + j,
|>         I conclude that it should work.
|>         Is this true?

I think so.

--

Valentin Bonnard                mailto:bonnardv@pratique.fr
info about C++/a propos du C++: http://www.pratique.fr/~bonnardv/
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]