[GiNaC-list] GiNaC and malloc()
Igor Khavkine
igor.kh at gmail.com
Tue Jun 27 23:10:40 CEST 2006
On 6/27/06, Richard B. Kreckel <kreckel at ginac.de> wrote:
> I don't think you can get around those "extra steps" you mention. In
> C++, one has placement new for directed allocating. But you would have
> to write some additional code between #ifdefs to use it instead of
> malloc(3). And even doing so would not spare you extra destructor calls
> to avoid leaks. This function does not leak:
Ah, I didn't know about this feature of the new operator. It seems to
do exactly what I want. Thanks!
> void f(size_t n)
> {
> char buf[n*sizeof(GiNaC::ex)]; // you manage that memory!
> for (size_t i=0; i<n; ++i) {
> new(&buf[i*sizeof(GiNaC::ex)]) GiNaC::ex(2);
> }
>
> //...
> cout << *reinterpret_cast<GiNaC::ex*>(&buf[0 * sizeof(GiNaC::ex)])
> << endl;
> //...
>
> // It is your responsibility to keep track of the reference counts:
Calling destructors is not a big deal. Do you mean anything else by
"reference counts"? I couldn't find any documented way to access the
reference counting information that GiNaC keeps internally.
> for (size_t i=0; i<n; ++i) {
> reinterpret_cast<GiNaC::ex*>(&buf[i*sizeof(GiNaC::ex)])->~ex();
> }
> }
>
> Not sure if this helps.
Very much so. Thanks again!
Igor
More information about the GiNaC-list
mailing list