enamel
enamel

Reputation: 81

Array declaration of size one

Traversing the 'zval' structure in the source code of Zend , I saw this: // zend_types.h

struct _zend_string {
   zend_refcounted_h gc;
   zend_ulong        h;                /* hash value */
   size_t            len;
   char              val[1];
};

This structure is used to store string , but 'char val[1] ' seems awkward How it is used ?

Upvotes: 2

Views: 74

Answers (1)

Kami Kaze
Kami Kaze

Reputation: 2080

Something like this is used to give access to an array of a length unknown at compile time. The struct gets its memory from malloc with a size bigger than the struct. So the array can be used to access the excess memory. lenis important to stay in the limits.

It is strange that it is a 1 element array, 0-element arrays were common for this until variable length arrays (val[]) were introduced in c99.

Upvotes: 2

Related Questions