# destroying an array and using a new one

- Thread: https://externals.io/message/60910
- Original archive: https://news-web.php.net/php.internals/60910
- Started by Yader Hernandez on 2012-06-21T03:39:38+00:00
- Messages: 3
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #60910 — Yader Hernandez — 2012-06-21T03:39:38+00:00

URL: https://externals.io/message/60910#60910

I'm trying to reverse an array but I keep getting bizarre results, such as
seg faults

0x00000000006d0940 in gc_remove_from_buffer (zv=0x7fffffffb550) at
/home/yaderbh/php-5.4.3/Zend/zend_gc.h:189
189 root->next->prev = root->prev;

or I'll get "PHP Fatal error:  Allowed memory size of" errors when trying
to manipulate the array in a script.

Basically what I'm doing is creating a pointer to zval, allocating and
creating the zval into an array, traversing the existing array and
calling add_index_zval. The position of the index is the current size and
work my way down.


    hash_table = Z_ARRVAL_P(intern->elements);



    ALLOC_INIT_ZVAL(new_elements);

    array_init(new_elements);



    zend_hash_internal_pointer_reset_ex(hash_table, &pos);



    i = intern->size;



    while (zend_hash_get_current_data_ex(hash_table, (void **)&current,
&pos) == SUCCESS) {
        tmp = **current;



        zval_copy_ctor(&tmp);



        add_index_zval(new_elements, i, &tmp);

        i--;



        zend_hash_move_forward_ex(hash_table, &pos);

    }



    zval_ptr_dtor(&intern->elements);



    intern->elements = new_elements;


Can anyone spot the problem?

---

## #60911 — Laruence — 2012-06-21T03:51:47+00:00

URL: https://externals.io/message/60910#60911 · In reply to: #60910

On Thu, Jun 21, 2012 at 11:39 AM, Yader Hernandez
<yader.hernandez@gmail.com> wrote:
> I'm trying to reverse an array but I keep getting bizarre results, such as
> seg faults
>
> 0x00000000006d0940 in gc_remove_from_buffer (zv=0x7fffffffb550) at
> /home/yaderbh/php-5.4.3/Zend/zend_gc.h:189
> 189 root->next->prev = root->prev;
>
> or I'll get "PHP Fatal error:  Allowed memory size of" errors when trying
> to manipulate the array in a script.
>
> Basically what I'm doing is creating a pointer to zval, allocating and
> creating the zval into an array, traversing the existing array and
> calling add_index_zval. The position of the index is the current size and
> work my way down.
>
>
>    hash_table = Z_ARRVAL_P(intern->elements);
>
>
>
>    ALLOC_INIT_ZVAL(new_elements);
>
>    array_init(new_elements);
>
>
>
>    zend_hash_internal_pointer_reset_ex(hash_table, &pos);
>
>
>
>    i = intern->size;
>
>
>
>    while (zend_hash_get_current_data_ex(hash_table, (void **)&current,
> &pos) == SUCCESS) {
>        tmp = **current;

Hi:
tmp is a stack allocted variable.  when this scope is dead,  *(&tmp)
is undefined value.

you should use a heap allocated variable, or just add_ref the current zval

thanks
>
>
>
>        zval_copy_ctor(&tmp);
>
>
>
>        add_index_zval(new_elements, i, &tmp);
>
>        i--;
>
>
>
>        zend_hash_move_forward_ex(hash_table, &pos);
>
>    }
>
>
>
>    zval_ptr_dtor(&intern->elements);
>
>
>
>    intern->elements = new_elements;
>
>
> Can anyone spot the problem?



-- 
Laruence  Xinchen Hui
http://www.laruence.com/

---

## #60912 — Yader Hernandez — 2012-06-21T05:05:01+00:00

URL: https://externals.io/message/60910#60912 · In reply to: #60911

> On Thu, Jun 21, 2012 at 11:39 AM, Yader Hernandez
> <yader.hernandez@gmail.com> wrote:
> > […]
> as
> > […]
>
> Hi:
> tmp is a stack allocted variable.  when this scope is dead,  *(&tmp)
> is undefined value.
>
> you should use a heap allocated variable, or just add_ref the current zval
>
> thanks
> > […]
>
>
>
> --
> Laruence  Xinchen Hui
> http://www.laruence.com/
>

Of course, I forgot to use Z_ADDREF_P. But I'm still having an issue. All
of the elements in the new array are now null. zend_hash_index_find is
returning SUCCESS.

Apart from adding Z_ADDREF_P, is there anything I should be doing?

thanks
