【问题标题】:Passing data structure through glib queue通过 glib 队列传递数据结构
【发布时间】:2011-06-06 17:08:17
【问题描述】:

我还有一个指针问题,如果你能帮我解决这个问题,我会很高兴

我有这个结构:

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);

但应用程序因分段错误而失败。

谁能帮帮我?我没有得到整个指针问题。

【问题讨论】:

  • 我只是重写这段代码,所以我想尽可能避免更改现有代码

标签: c pointers queue segmentation-fault glib


【解决方案1】:

您正在将分配在堆栈上的内存推到队列的末尾:

uint8_t *reconstructed[3];

然后尝试将其从队列中拉到其他地方。当您将事物从队列中拉出时,您的三个元素 reconstructed 数组的堆栈空间几乎肯定会用于其他用途。我认为您必须将 reconstructed 更改为 uint8_t ** 并将其分配到堆上:

unint8_t **reconstructed;
reconstructed = malloc(3 * sizeof(uint8_t *));
/* Proceed as before. */

这将使reconstructed 内存在声明它的函数之外保持有效(当然排除其他错误)。当您将其从队列中拉出并完成时,您还必须释放您的 reconstructed 值(及其元素);释放时,请务必考虑reconstructed 元素上的奇数偏移量。

【讨论】:

    【解决方案2】:

    好的,我也想通了 无论如何,我出于另一个原因创建了我需要的包装器

    // 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
    

    非常感谢您的意见和回复

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-04
      • 2021-07-30
      • 2012-07-06
      • 1970-01-01
      • 2013-06-04
      相关资源
      最近更新 更多