结构体的地址与其第一个成员的地址相同吗?
考虑我有如下结构:
struct Bitmask
{
unsigned char payload_length: 7;
unsigned char mask: 1;
unsigned char opcode: 4;
unsigned char rsv3: 1;
unsigned char rsv2: 1;
unsigned char rsv1: 1;
unsigned char fin: 1;
};
const char* payload = "Hello";
const size_t payload_length = strlen(payload);
Bitmask* header = new Bitmask();
header->fin =1;
header->rsv1 = 0;
header->rsv2 = 0;
header->rsv3 = 0;
header->opcode = 1;
header->mask = 0;
header->payload_length = payload_length;
iovec iov[2];
iov[0].iov_base = (char*)header;
iov[0].iov_len = sizeof (header);
iov[1].iov_base = (char *)payload;
iov[1].iov_len = strlen(payload);
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("iov[0].length = %d\niov[1].length = %d\n"),
iov[0].iov_len,
iov[1].iov_len));
size_t bytes_xfered;
client_stream_.sendv_n (iov, 2, 0, &bytes_xfered);
cout << "Transfered " << bytes_xfered << " byte(s)" << std::endl;
我正在使用适当的值初始化它。最后,我想将结构转换为 char*,这样我就可以附加我的有效负载(即 char* 消息)并通过 websocket 连接发送它。
Consider I have Struct like the following:
struct Bitmask
{
unsigned char payload_length: 7;
unsigned char mask: 1;
unsigned char opcode: 4;
unsigned char rsv3: 1;
unsigned char rsv2: 1;
unsigned char rsv1: 1;
unsigned char fin: 1;
};
const char* payload = "Hello";
const size_t payload_length = strlen(payload);
Bitmask* header = new Bitmask();
header->fin =1;
header->rsv1 = 0;
header->rsv2 = 0;
header->rsv3 = 0;
header->opcode = 1;
header->mask = 0;
header->payload_length = payload_length;
iovec iov[2];
iov[0].iov_base = (char*)header;
iov[0].iov_len = sizeof (header);
iov[1].iov_base = (char *)payload;
iov[1].iov_len = strlen(payload);
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("iov[0].length = %d\niov[1].length = %d\n"),
iov[0].iov_len,
iov[1].iov_len));
size_t bytes_xfered;
client_stream_.sendv_n (iov, 2, 0, &bytes_xfered);
cout << "Transfered " << bytes_xfered << " byte(s)" << std::endl;
I am initializing it with appropriate values. Finally, I want to convert the struct into char* so I can append my payload (which is char* message) and send it over a websocket connection.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,这实际上是 C 和 C++ 标准强制要求的。从C标准来看:
struct
的大小应为两个字节。不过,您不应该将指向它的指针转换为char*
:相反,您应该使用memcpy
将您的位掩码
复制到您通过网络发送的缓冲区中。编辑由于您将分散收集 I/O 与
iovec
结合使用,因此无需将Bitmask
转换为任何内容:iov_base 是
void*
,因此您只需设置iov[0].iov_base = header;
注意:这仅在您的
struct
不包含虚函数、基类、等等(谢谢,蒂莫)。EDIT2
为了在
struct
中获取 {0x81, 0x05},您应该更改结构元素的顺序,如下所示:Yes, this is actually mandated by the C and C++ standards. From the C standard:
The size of your
struct
should be two bytes. You should not convert a pointer to it tochar*
, though: instead, you should usememcpy
to copy yourBitmask
into the buffer that you send over the network.EDIT Since you use scatter-gather I/O with
iovec
, you do not need to castBitmask
to anything:iov_base
isvoid*
, so you can simply setiov[0].iov_base = header;
Note: This works only as long as your
struct
does not contain virtual functions, base classes, etc. (thanks, Timo).EDIT2
In order to get {0x81, 0x05} in your
struct
, you should change the order of structure elements as follows:是的,也不是。
一般来说,这是正确的(如 dasblinkenlight 所解释的),但它特别不适用于位字段。根据 C++11 9.6/3,“没有指向位域的指针”,因此它们也没有地址。显然,如果没有“适当的转换”,“指向结构对象的指针,适当转换,指向其初始成员”就会崩溃。
Yes and no.
In general, this is true (as dasblinkenlight explains), but it specifically doesn't hold for bitfields. Per C++11 9.6/3 "there are no pointers to bitfields" so they don't have addresses, either. And obviously, "A pointer to a structure object, suitably converted, points to its initial member" breaks down if there is no "suitable conversion".
如果使用适当的强制转换,结构的地址与其第一个成员的地址相同。给定以下
struct my_struct
声明,如果item
的类型为 structThe address of a structure is the same as the address of its first member, provided that the appropriate cast is used. Given the below declaration of
struct my_struct
, ifitem
is of type struct