使用memcpy()后引用结构指针时出错

时间:2014-09-25 02:07:25

标签: c pointers memcpy distributed-system

void Process_joinreq(void *env, char *data, int size)
{
    // env = the node who has received the msg. Introducer
    member *node = (member *) env;

    /* address of the member who sent a join request to the introducer.*/
    address *addr = (address *)data;

    // memberlist is a structure
    size_t msgsize = sizeof(messagehdr) + sizeof(struct memberlist);
    messagehdr *msg=malloc(msgsize);

    printf("\nIn Process_joinreq function, Sending reply from the introducer to ");
    printf("%d.%d.%d.%d:%d \n", addr->addr[0], addr->addr[1],
            addr->addr[2], addr->addr[3], *(short *)&addr->addr[4]);

    msg->msgtype=JOINREP;

    // add the new nodes row in the membership list and call the logNodeAdd function
    memcpy(&node->membershiplist[node->countofmembers].addr,addr,sizeof(address));
    node->membershiplist[node->countofmembers].heartbeatcounter += 1;
    node->membershiplist[node->countofmembers].timestamp = getcurrtime();
    node->membershiplist[node->countofmembers].isfailed = 0;
    node->countofmembers++;
    logNodeAdd(&node->addr,addr);

    printf("\nThe number of members in the list of introducer is d\n",node->countofmembers);

    //msg should be the membership list.
    memcpy((char *)(msg+1), &node->membershiplist, sizeof(memberlist));
    /* send JOINREQ message to introducer member. */
    MPp2psend(&node->addr, addr, (char *)(msg), msgsize);

    free(msg);
    return;
}

我正在尝试在分布式系统中实现成员资格协议。上面的代码描述了一个函数,用于处理除尝试加入网络的介绍者进程之外的进程的请求。我能够从介绍人那里获得对已成功加入网络的每个流程的回复。

第二步涉及将成员列表从介绍人发送到所有剩余节点。当我尝试访问Process_joinrep函数中的node-> membership列表时,它具有垃圾值。我在这做错了什么?

0 个答案:

没有答案