Re: Extension C code help please
| From: | (Uwe Steinmann) | Date: | Tue, 13 Jul 2004 09:43:17 +0000 |
| Subject: | Re: Extension C code help please | ||
| References: | 1 | Groups: | php.pecl.dev |
| Request: | Send a blank email to pecl-dev+get-951@lists.php.net to get a copy of this message | ||
On Tue, Jul 13, 2004 at 10:06:07AM +0100, Chris Hemmings wrote:
> Hello,
>
> Not sure if this is the right group to post to but I need some help on a
> PHP Extension that I have written.
> It is used to allow PHP to query VerityK2 servers and what I have done
> so far works, I just need to sort out a few loose ends.
>
> This bit I am stuck on is the following:
>
> CSTRING--pointer--A pointer to a null-terminated character string.
>
> Near the top of my code I define an array of CSTRINGS that will contain
> the field and collection names.
>
> CSTRING fieldNm[1000];
> CSTRING collectionNm[1000];
>
> I then go through an array that I pass from the PHP function call and
> copy the contents of each element of the array into fieldNm or collectionNm.
>
> Below is the code that copies the fields into fieldNm.
>
> for (zend_hash_internal_pointer_reset(hfields);
> zend_hash_get_current_data(hfields,(void**)&entry)==SUCCESS;
I guess you just forgot the if(..) here?
> zend_hash_move_forward(hfields)){
> convert_to_string_ex(entry);
> fieldNm[fieldcounter]=malloc(sizeof(Z_STRVAL_PP(entry)));
> fieldNm[fieldcounter]=Z_STRVAL_PP(entry);
This will just put a pointer into fieldNm[fieldcounter] and does not
fill your previously allocated memory. You probably want to use
strdup (estrdup).
> fieldcounter++;
> }
>
> My problem is (apart from being rubbish a C) is that fieldNm's size is
> defined before I actually know how many elements there are. I have set
> it arbitrarily high to 1000 and this
> to work, but, it would be nice to get better I suppose.
Use zend_hash_num_elements(hfields) to determine the number of
elements in the passed array. Than use malloc to allocate memory
for your internal array of strings. You may consider using
php's emalloc instead of malloc. Depending on what you do with
the array later on.
Uwe
--
MMK GmbH, Universitaetsstr. 11, 58097 Hagen
Uwe.Steinmann@mmk-hagen.de
Tel: +2331 840446 Fax: +2331 843920