[英]Passing data structure through glib queue
我有另一个指针问题,所以如果你能帮我解决这个问题,我会很高兴
我有这个结构:
uint8_t *reconstructed[3];
reconstructed[0] = buff_offset + (uint8_t *) malloc (buff_size);
reconstructed[1] = buff_offset + (uint8_t *) malloc (buff_size);
reconstructed[2] = buff_offset + (uint8_t *) malloc (buff_size);
我这样使用这个变量:
y4m_write_frame (fd_out, &ostreaminfo, &oframeinfo, reconstructed);
我的任务是并行化这个应用程序,出于几个原因,我需要将此结构放在 GLib 队列中,并在一些操作后使用它。
所以我把它放在队列中:
g_queue_push_tail(queue, (gpointer) reconstructed);
但现在我不知道如何从那里得到它。 我试过了:
uint8_t * const * frame = (uint8_t * const *) g_queue_pop_head(queue);
y4m_write_frame (fd_out, &ostreaminfo, &oframeinfo, frame);
但是应用程序因分段错误而失败。
任何人都可以帮助我吗? 我没有得到整个指针问题。
您将在堆栈上分配的 memory 推到队列的末尾:
uint8_t *reconstructed[3];
然后试图将其从队列中拉到其他地方。 当你把东西从队列中拉出来的时候,你的三元素reconstructed
数组的堆栈空间几乎肯定会被用于其他东西。 我认为您必须将reconstructed
更改为uint8_t **
并将其分配到堆上:
unint8_t **reconstructed;
reconstructed = malloc(3 * sizeof(uint8_t *));
/* Proceed as before. */
这将使reconstructed
的 memory 在声明它的 function 之外保持有效(当然排除其他错误)。 当您将其从队列中拉出并完成时,您还必须释放reconstructed
的值(及其元素); 释放时,一定要考虑到reconstructed
元素上的奇数偏移。
好的,所以我也发现我创建了我需要的包装器是出于另一个原因
// structure
struct wrapper {
uint8_t *reconstructed[3];
int should_reconstruct;
int run_count;
};
// malloc wrapper
struct wrapper* link = (struct wrapper*) malloc(sizeof(struct wrapper));
link->reconstructed[0] = buff_offset + (uint8_t *) malloc (buff_size);
link->reconstructed[1] = buff_offset + (uint8_t *) malloc (buff_size);
link->reconstructed[2] = buff_offset + (uint8_t *) malloc (buff_size);
// add additional informations to wrapper
link->should_reconstruct = 0;
link->run_count = run_count;
g_queue_push_tail(queue, (gpointer) link);
当我需要在不同的命名空间中使用它时,我只是
struct wrapper* frame = (struct wrapper*) g_queue_pop_head(queue);
y4m_write_frame (fd_out, &ostreaminfo, &oframeinfo, frame->reconstructed);
// rest of the code
非常感谢您的意见和回复
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.