Sisyphus [28/05/02 15:04 +1000]:
> (Still uncertain as to the circumstances that would require use of 'Newc'
> ...... if there's someone there that would care to enlighten me :-)
Here are the definitions. These are defined in handy.h in the Perl source
distro:
#define New(x,v,n,t) (v = (t*)safemalloc((MEM_SIZE)((n)*sizeof(t))))
#define Newc(x,v,n,t,c) (v = (c*)safemalloc((MEM_SIZE)((n)*sizeof(t))))
#define Newz(x,v,n,t) (v = (t*)safemalloc((MEM_SIZE)((n)*sizeof(t)))), \
memzero((char*)(v), (n)*sizeof(t))
As you can see, you only need Newc() if you want to assign a pointer of a
different type. For example:
/* A structure */
typedef struct {
int something;
} parent_t, *parent_ptr;
/* A structure which "inherits" from parent_t. You can loosely think of
* inheritance as having the same members as your parent, plus some more
* stuff that the parent doesn't know about. */
typedef struct {
int something;
int child;
} child_t, *child_ptr;
/* Now we'll define a parent pointer... */
{
parent_ptr pptr;
/* ... and make it point to a newly-created child_t object... */
Newc(193, pptr, 1, child_t, parent_ptr);
/* ... and prove the point: */
printf("This is a pointer to a parent_t: pptr->something = %i\n",
pptr->something);
printf("This is a compile error: pptr->child = %i\n",
pptr->child);
Hope that helps!
Later,
Neil