Karim Belabas on Sun, 06 Jun 2010 09:20:30 +0200


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: C++ new and delete like malloc and free?


* Daniel Allcock [2010-06-05 21:04]:
> I'll probably be spamming the list for a little while since I'm
> working on a project, so thanks for your patience.

No problemo :-)

> I'm developing a program in C++ that uses libpari.  I know that pari
> has wrappers around malloc and free, namely pari_malloc and pari_free,
> and that using them protects against stack corruption problems in the
> presence of a SIGINT.
> 
> The docs say pari is C++ compatible, and there is no mention of
> analogous problems for operators new and delete in C++.  Is the same
> issue present?  If so, what do I do to address it?  Just trying to be
> careful.

The same (very rare!) issue is present, but need only be a concern under gp.
Or if your project also intercepts SIGINT, longjmp'ing to some other
context and letting the program go on.

I guess you can write analogous wrappers pari_new() and pari_delete(),
but the genuine problem is that longjmp() is dangerous in C++ programs 
(undefined behaviour if replacing the setjmp/longjmp by catch/throw
would destroy any automatic object). One should really use proper C++
exceptions is this case.

This is not a problem when you call a PARI library function from a C++
program (no automatic objects would be destroyed when longjmp'ing out of
a PARI function), but it does become one if you use gp or a gp-like
shell using longjmp to recover from errors (see example/minigp.c) to
call C++ code.

Cheers,

    K.B.
--
Karim Belabas, IMB (UMR 5251)  Tel: (+33) (0)5 40 00 26 17
Universite Bordeaux 1          Fax: (+33) (0)5 40 00 69 50
351, cours de la Liberation    http://www.math.u-bordeaux.fr/~belabas/
F-33405 Talence (France)       http://pari.math.u-bordeaux.fr/  [PARI/GP]
`