<legend id='0n3dj'><style id='0n3dj'><dir id='0n3dj'><q id='0n3dj'></q></dir></style></legend>
    <tfoot id='0n3dj'></tfoot>

      • <bdo id='0n3dj'></bdo><ul id='0n3dj'></ul>

    1. <i id='0n3dj'><tr id='0n3dj'><dt id='0n3dj'><q id='0n3dj'><span id='0n3dj'><b id='0n3dj'><form id='0n3dj'><ins id='0n3dj'></ins><ul id='0n3dj'></ul><sub id='0n3dj'></sub></form><legend id='0n3dj'></legend><bdo id='0n3dj'><pre id='0n3dj'><center id='0n3dj'></center></pre></bdo></b><th id='0n3dj'></th></span></q></dt></tr></i><div id='0n3dj'><tfoot id='0n3dj'></tfoot><dl id='0n3dj'><fieldset id='0n3dj'></fieldset></dl></div>

        <small id='0n3dj'></small><noframes id='0n3dj'>

      1. 结构的地址是否与其第一个成员的地址相同?

        Is a struct#39;s address the same as its first member#39;s address?(结构的地址是否与其第一个成员的地址相同?)
      2. <legend id='fZhqE'><style id='fZhqE'><dir id='fZhqE'><q id='fZhqE'></q></dir></style></legend>

        <small id='fZhqE'></small><noframes id='fZhqE'>

              <bdo id='fZhqE'></bdo><ul id='fZhqE'></ul>
              <tfoot id='fZhqE'></tfoot>
                  <tbody id='fZhqE'></tbody>
                <i id='fZhqE'><tr id='fZhqE'><dt id='fZhqE'><q id='fZhqE'><span id='fZhqE'><b id='fZhqE'><form id='fZhqE'><ins id='fZhqE'></ins><ul id='fZhqE'></ul><sub id='fZhqE'></sub></form><legend id='fZhqE'></legend><bdo id='fZhqE'><pre id='fZhqE'><center id='fZhqE'></center></pre></bdo></b><th id='fZhqE'></th></span></q></dt></tr></i><div id='fZhqE'><tfoot id='fZhqE'></tfoot><dl id='fZhqE'><fieldset id='fZhqE'></fieldset></dl></div>

                1. 本文介绍了结构的地址是否与其第一个成员的地址相同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  考虑我有如下结构:

                  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
                  iov[1].length = %d
                  "),
                              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 连接发送它.

                  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.

                  推荐答案

                  结构的地址是否与其第一个成员的地址相同?

                  Is a struct's address the same as its first member's address?

                  是的,这实际上是 C 和 C++ 标准规定的.来自 C 标准:

                  Yes, this is actually mandated by the C and C++ standards. From the C standard:

                  6.7.2.1-13.一个指向结构对象的指针,经过适当的转换,指向它的初始成员

                  6.7.2.1-13. A pointer to a structure object, suitably converted, points to its initial member

                  struct 的大小应该是两个字节.但是,您不应将指向它的指针转换为 char*:相反,您应该使用 memcpy 将您的 Bitmask 复制到您通过网络发送的缓冲区中.

                  The size of your struct should be two bytes. You should not convert a pointer to it to char*, though: instead, you should use memcpy to copy your Bitmask into the buffer that you send over the network.

                  EDIT 由于您将分散-聚集 I/O 与 iovec 一起使用,因此您无需将 Bitmask 转换为任何内容:iov_basevoid*,所以你可以简单地设置 iov[0].iov_base = header;

                  EDIT Since you use scatter-gather I/O with iovec, you do not need to cast Bitmask to anything: iov_base is void*, so you can simply set iov[0].iov_base = header;

                  注意:这仅在您的 struct 不包含虚函数、基类等时才有效(感谢 Timo).

                  Note: This works only as long as your struct does not contain virtual functions, base classes, etc. (thanks, Timo).

                  EDIT2

                  为了在您的 struct 中获得 {0x81, 0x05},您应该按如下方式更改结构元素的顺序:

                  In order to get {0x81, 0x05} in your struct, you should change the order of structure elements as follows:

                  struct Bitmask {
                      unsigned char opcode: 4; 
                      unsigned char rsv3: 1; 
                      unsigned char rsv2: 1; 
                      unsigned char rsv1: 1; 
                      unsigned char fin: 1; 
                      unsigned char payload_length: 7; 
                      unsigned char mask: 1;
                  }
                  

                  这篇关于结构的地址是否与其第一个成员的地址相同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  What is inside .lib file of Static library, Statically linked dynamic library and dynamically linked dynamic library?(静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么?)
                  How do I load a C DLL from the SXS in Python?(如何从 Python 中的 SXS 加载 C DLL?)
                  Can Cython code be compiled to a dll so C++ application can call it?(Cython 代码可以编译成 dll 以便 C++ 应用程序可以调用它吗?)
                  Delay Loading DLLs(延迟加载 DLL)
                  Throwing C++ exceptions across DLL boundaries(跨 DLL 边界抛出 C++ 异常)
                  Loading a dll from a dll?(从 dll 加载 dll?)
                  <tfoot id='Xj5MR'></tfoot>

                    <small id='Xj5MR'></small><noframes id='Xj5MR'>

                      <legend id='Xj5MR'><style id='Xj5MR'><dir id='Xj5MR'><q id='Xj5MR'></q></dir></style></legend>

                          • <bdo id='Xj5MR'></bdo><ul id='Xj5MR'></ul>
                            <i id='Xj5MR'><tr id='Xj5MR'><dt id='Xj5MR'><q id='Xj5MR'><span id='Xj5MR'><b id='Xj5MR'><form id='Xj5MR'><ins id='Xj5MR'></ins><ul id='Xj5MR'></ul><sub id='Xj5MR'></sub></form><legend id='Xj5MR'></legend><bdo id='Xj5MR'><pre id='Xj5MR'><center id='Xj5MR'></center></pre></bdo></b><th id='Xj5MR'></th></span></q></dt></tr></i><div id='Xj5MR'><tfoot id='Xj5MR'></tfoot><dl id='Xj5MR'><fieldset id='Xj5MR'></fieldset></dl></div>
                              <tbody id='Xj5MR'></tbody>