c - 在结构体中存储 void 指针

标签 c pointers struct zeromq

我在尝试初始化指向 0mq 上下文和套接字的指针结构的代码中遇到段错误。 main 方法中注释掉的代码可以工作,但它仅使用局部变量。我想初始化它们并在结构中传递它们,但我的 google foo 不知道如何正确执行此操作。

#include "zhelpers.h"
#include <stdio.h>
#include <stdlib.h>
#include <zmq.h>

struct publisher{
    void *handle;
    void *context;
};

void init_publisher(struct publisher *p);
void destroy_publisher(struct publisher *p);
void publish(struct publisher *p,char *msg);

void init_publisher(struct publisher *p)
{
    p = (struct publisher *)malloc(sizeof(struct publisher));
    p->context = malloc(sizeof(void *));
    p->handle = malloc(sizeof(void *));
    void *context = zmq_ctx_new();
    void *handle = zmq_socket(context,ZMQ_PUB);
    zmq_bind(handle, "tcp://*:5556");
    zmq_bind(handle, "ipc://feed.ipc");
    p->context = context;
    p->handle = handle;
}

void destroy_publisher(struct publisher *p)
{
    zmq_close(p->handle);
    zmq_ctx_destroy(p->context);
    free(p->handle);
    free(p->context);
    free(p);
}

void publish(struct publisher *p,char *msg)
{
    s_send(p->handle, msg);
}

int main(void)
{
/**
    void *context = zmq_ctx_new();
    void *publisher = zmq_socket(context, ZMQ_PUB);
    int rc = zmq_bind(publisher, "tcp://*:5556");
    assert(rc == 0);
    rc = zmq_bind(publisher, "ipc://weather.ipc");
    assert(rc == 0);
    printf("Started Weather Server...\n");

    srandom((unsigned) time (NULL));
    int zipcode, temperature, relhumidity;
    zipcode = randof(100000);
    temperature = randof (215) - 80;
    relhumidity = randof (50) + 10;

    char update[20];
    sprintf(update, "%05d %d %d", zipcode, temperature, relhumidity);
    s_send(publisher, update);
    zmq_close(publisher);
    zmq_ctx_destroy(context);
*/

    struct publisher *p;
    init_publisher(p);
    printf("Setup pub\n");

    srandom((unsigned) time (NULL));
    int zipcode, temperature, relhumidity;
    zipcode = randof(100000);
    temperature = randof (215) - 80;
    relhumidity = randof (50) + 10;
    char update[20];
    sprintf(update, "%05d %d %d", zipcode, temperature, relhumidity);
    publish(p,update);
    printf("Published Message\n");

    destroy_publisher(p);
    printf("Destroyed publisher\n");
    return 0;
}

最佳答案

此代码中似乎没有任何内容会导致其崩溃。 (假设您知道所有 zmq_... 内容的工作原理。)

如果您准确地告诉我们错误发生的位置,将会有所帮助,但我的猜测是错误发生在这段代码之外。

你看,你将 structpublisher *p 传递给你的 init_publisher() 函数,但随后你在内部为 p 分配内存该方法(这使得传递 p 毫无意义),然后您就不会返回 p。因此,调用 init_publisher() 的代码可能期望 p 被初始化,但事实并非如此。 p 指向的内存只是在 init_publisher() 函数中本地分配和泄漏。

因此,无需传递 p,只需让函数声明它并返回它即可。

或者,如果调用者已经分配了 p,则不要在 init_publisher() 中再次分配它。

<小时/>

另请注意,语句 p->context = malloc(sizeof(void *)); 是不必要的,它们会泄漏少量内存,因为您继续覆盖这些语句结构体成员。

关于c - 在结构体中存储 void 指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34258602/

相关文章:

dictionary - 如何使用 GO 构建结构图并向其附加值?

c++ - const 指针的 vector ?

c - Emacs 口齿不清的问题

c++ - 获取 C++ 引用的地址以传递给指针

c++ - Eclipse CDT 包含文件夹

c++ - 在 C++ 中初始化指针是强制性的吗?

c++ - Boost property_tree 用于存储指针

c - 结构中 restrict 关键字的行为

c - "use of undeclared identifier"将 void 指针类型转换为结构指针时

c - c语言中如何比较两个矩阵的元素