Skip to content
Back to formatted view

Raw Message

Message-ID: <C03F8157B6DECC428DE8D36DC5F9244114F1308EAF@DFW1MBX03.mex07a.mlsrvr.com>
Date: 2013-07-23T16:50:04Z
From: Steve Jaffe
Subject: [Rcpp-devel] Rcpp-devel] What is the best practice to expose a C structure from 3rd party library into R?

on: Tue, 23 Jul 2013 11:37:25 -0500
Dirk Eddelbuettel <edd at debian.org> wrote:

> | A singleton does make things easier. However, I still want to know how to
> | expose C structure with provided `free`-like function because sometimes we
> | cannot use singleton.
> 
> Manage your objects carefully, and don't call free() on NULL pointers.

According to the C standard, it is safe to call free on a null pointer. Similarly, according to the C++ standard, it is safe to call 'delete' on a null pointer.

Thus there is no need ever to test a pointer for null before free-ing/delete-ing. 

However, it *is* good practice to assign null to a pointer *after* free-ing/delete-ing, since that ensures that any subsequent calls to free/delete will be safe.

What is dangerous is calling free/delete twice on the same *non-null* pointer.

Regards,
Steve

Steve Jaffe
sjaffe at riskspan.com