Smart Pointer implementation in C

Possible duplicate:
Smart pointers / secure memory management for C?

I have a built-in application where I place an object in dynamic memory and transfer it to other modules.

I would like to create a smart pointer to this object. In C ++, there are many examples of the use and implementation of smart pointers.

I search only in C only a smart pointer.

Thank.

+5
source share
3 answers

, , (, , ) - , @KennyTM. - . reference() unreference(). ?

. , SO: / C?

+2

"" , , , , ++.

.

smartpointer.h:

typedef struct sp smartpointer;

smartpointer new(void *content, size_t s);
int          delete(smartpointer p)
void*        dereference(smartpointer p);
/* ... */

smartpointer.c

/* contains the definition of "struct sp" and the implementation of the 
   the functions */

, , , dereference, , , .

, , , .

+1

, ( ) 2 :

  • ( ) /
  • ( ..)

API, / .

But the first element is where C ++ really shines and language support for constructors / destructors (the heart of RAII). It is the fact that constructors and destructors are invoked through code that is automatically inserted by the compiler into the correct places that make the magic work. Without the native language support for CTOR and DTOR, you will simply approximate the effect or rely on the user to "do the right thing" when necessary.

0
source

All Articles