在结构中存储空指针

Storing void pointers in a struct

我在尝试初始化指向 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_... 东西是如何工作的。)

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

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

所以,不用传递 p,只需让函数声明它并 return 它。

或者,如果调用者已经分配了 p,则不要从 init_publisher().

中重新分配它

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

问题是传递的指针和你malloc()输入的指针不一样。传递的指针包含与原始指针相同的地址,可能是无效地址,但指针本身的地址不同,因为在 中你只能按值传递变量,因此指针被复制。

这意味着当您在函数内部重新分配 p 时,函数外部的 p 不会改变。如果它是在外面分配的,你只是使用函数来访问它的成员,那就不一样了。

您也不需要 malloc() 每个要使用的指针,问题是它必须在取消引用之前指向有效地址。当你想请求新的未初始化内存时,你可以使用 malloc() 否则你只需将指针指向一个有效地址,以便定义取消引用,一个使用指针而不 malloc() 的例子是

int *pointer;
int value;
value = 4;
pointer = &value; // Now `pointer' points to `value's` address
*pointer = 3;
printf("%d\n", value);

编写函数的一种方法是

int
init_publisher(struct publisher **pp)
{
    struct publisher *p;
    *pp = malloc(sizeof(struct publisher));
    if (*pp == NULL)
        return -1;
    p = *pp;
    p->context = zmq_ctx_new();
    p->handle = zmq_socket(context,ZMQ_PUB);
    if (p->handle != NULL) /* Just in case, do not dereference a NULL pointer */
    {
        zmq_bind(p->handle, "tcp://*:5556");
        zmq_bind(p->handle, "ipc://feed.ipc");
    }
    return 0;
}

然后你可以像这样使用它

struct publisher *p;
if (init_publisher(&p) != 0)
    do_something_there_was_an_error();
/* Continue using `p' */

请注意,该函数正在返回一个指示分配是否成功的值。通常 malloc() 不会失败,但这并不意味着您应该忽略可能的失败。

我说如果你先分配 p 的意思是,如果你改为这样做

struct publisher *p;
p = malloc(sizeof(*p));
if (p == NULL)
    return handle_error();
init_publisher(p);

那么init_publisher()可能是

void
init_publisher(struct publisher *pp)
{
    void *context;
    void *handle;
    p->context = zmq_ctx_new();
    p->handle = zmq_socket(context,ZMQ_PUB);
    if (p->handle != NULL) /* Just in case, do not dereference a NULL pointer */
    {
        zmq_bind(p->handle, "tcp://*:5556");
        zmq_bind(p->handle, "ipc://feed.ipc");
    }
}

这可能就是您想要做的。