我试图为某些分布式应用程序编造一个代码以查看其工作情况,但是我遇到了错误。我正在尝试查看消息的工作
代码是
class Address {
public:
char addr[6];
Address() {}
Address(string address) {
size_t pos = address.find(":");
int id = stoi(address.substr(0, pos));
short port = (short)stoi(address.substr(pos + 1, address.size()-pos-1));
memcpy(&addr[0], &id, sizeof(int));
memcpy(&addr[4], &port, sizeof(short));
}
};
enum MsgTypes{
JOINREQ,
JOINREPLY,
DUMMYLASTMSGTYPE,
HEARTBEAT
};
/**
* STRUCT NAME: MessageHdr
*
* DESCRIPTION: Header and content of a message
*/
typedef struct MessageHdr {
enum MsgTypes msgType;
}MessageHdr;
typedef struct en_msg {
// Number of bytes after the class
int size;
// Source node
Address from;
// Destination node
Address to;
}en_msg;
void send(Address *myaddr, Address *toaddr, char *data, int size);
int main()
{
MessageHdr *msg;
size_t msgsize = sizeof(MessageHdr) + sizeof(Address) + sizeof(long) + 1;
int id=233;
short port =22;
long heartbeat=1;
string s=to_string(id)+to_string(port);
string s1=to_string(id+1)+to_string(port+1);
Address *addr= new Address(s);
Address *toaddr= new Address(s);
msg->msgType = JOINREQ;
memcpy((char *)(msg+1), addr, sizeof(addr));
memcpy((char *)(msg+1) + 1 + sizeof(addr), (char *)heartbeat, sizeof(long));
send(addr, toaddr, (char *)msg, msgsize);
}
void send(Address *myaddr, Address *toaddr, char *data, int size) {
en_msg *em;
static char temp[2048];
em = (en_msg *)malloc(sizeof(en_msg) + size);
em->size = size;
memcpy(&(em->from), &(myaddr), sizeof(em->from));
memcpy(&(em->to), &(toaddr), sizeof(em->from));
memcpy(em + 1, data, size);
cout<<em;
}
错误只是这一行:
分段故障(核心转储(
1(正如退休忍者在评论中所说,main
的第一行必须像
MessageHdr *msg = new MessageHdr();
因为msg->msgType = JOINREQ;
会导致未分配msg
错误。
2(第一个修复将无济于事,因为诸如
(char *)(msg+1)
这里msg
类型MessageHdr *
用作地址算术中的char *
。我的意思是,通过风格表达来计算地址是危险的
(char *)(msg+1) + 1
虽然msg
是MessageHdr*
类型,但(msg+1)
- 表示移动到下一个结构MessageHdr
,而转换为char*
后的额外+1
意味着移动到一个字节。我个人无法理解逻辑,而MessageHdr
结构只有一个enum
字段,并且通过如此奇怪的地址操作,您正在尝试将Address
类(或long int
值(的实例与该结构配合memcpy
。
结论:
需要非常大量的程序重新设计,编写注释并在操作中使用清晰的逻辑。