函数
GLibPtrArraysteal
自从:2.64
声明 [源]
gpointer*
g_ptr_array_steal (
GPtrArray* array,
gsize* len
)
描述 [源]
释放数组中的数据并将大小重置为零,同时保留底层数组以供其他地方使用,并将其返回给调用者。
请注意,如果数组以NULL终止,则即使数组的长度为零且pdata尚未分配,也可能返回NULL。
即使设置了,也永远不会在数组的当前内容上调用GDestroyNotify
函数,调用者负责释放数组元素。
使用示例
g_autoptr(GPtrArray) chunk_buffer = g_ptr_array_new_with_free_func (g_bytes_unref);
// Some part of your application appends a number of chunks to the pointer array.
g_ptr_array_add (chunk_buffer, g_bytes_new_static ("hello", 5));
g_ptr_array_add (chunk_buffer, g_bytes_new_static ("world", 5));
…
// Periodically, the chunks need to be sent as an array-and-length to some
// other part of the program.
GBytes **chunks;
gsize n_chunks;
chunks = g_ptr_array_steal (chunk_buffer, &n_chunks);
for (gsize i = 0; i < n_chunks; i++)
{
// Do something with each chunk here, and then free them, since
// `g_ptr_array_steal()` transfers ownership of all the elements and the
// array to the caller.
…
g_bytes_unref (chunks[i]);
}
g_free (chunks);
// After calling g_ptr_array_steal(), the pointer array can be reused for the
// next set of chunks.
g_assert (chunk_buffer->len == 0);
自从:2.64
此函数对语言绑定不可直接访问。