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

3

9

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.

Wavelength answered 13/2, 2012 at 1:40 Comment(0)
M
18

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

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

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

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 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;

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:

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;
}
Muggy answered 13/2, 2012 at 1:42 Comment(8)
In c++ this only holds true for POD structs.Formicary
Do I need to copy Bitmask member by member or following will do: char header_[2]; memcpy(header_,bitmask,2);Wavelength
@DarshanPuranik I did not realize that you were using scatter-gather I/O. See my edit.Muggy
iov[0].iov_base is char*. 'iov[0].iov_base = header;' then i get compile error c2440.Wavelength
@DarshanPuranik {0x81, 0x05} is not what gets written in the bits of Bitmask the way you specified. The data in the two bytes of Bitmask is {0x11, 0x0A}. With bit fields the bits are allocated lower-to-higher, hence the top bit is 0x01, second from the top bit is 0x02, and so on.Muggy
@DarshanPuranik ...error c2440. This is strange: opengroup says that it needs to be void*.Muggy
I am using ACE. aoc.nrao.edu/php/tjuerges/ALMA/ACE-5.5.2/html/ace/…Wavelength
let us continue this discussion in chatWavelength
B
1

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".

Brothel answered 13/2, 2012 at 12:56 Comment(0)
A
1

The 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, if item is of type struct

my_struct, then (char *)item == &item.wp_cval.


struct my_struct
{

      char wp_cval;
      short wp_font;
      short wp_psize;


}ar[ARSIZE];
Agni answered 13/2, 2012 at 13:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.