Memory management in Perl XS

To allocate memory in XS, use

void Newx(void* ptr, int nitems, type);
void Newxc(void* ptr, int nitems, type, cast);
void Newxz(void* ptr, int nitems, type);
Equivalent to malloc.
Equivalent to malloc with a cast, for C++.
Equivalent to calloc.

The following is a legacy interface:

void New(int id, void* ptr, int nitems, type );
void Newz(int id, void* ptr, int nitems, type );
void Newc(int id, void* ptr, int nitems, type , cast );

This is mentioned in the book "Extending and Embedding Perl" by Simon Cozens. It became obsolete in Perl 5.9.3.

There is also an interface to realloc

You can free the memory with something called Safefree.

Web links

Copyright © Ben Bullock 2009-2018. All rights reserved. For comments, questions, and corrections, please email Ben Bullock ( or use the discussion group at Google Groups. / Privacy / Disclaimer