从较大的数据类型(struct)转换为较小的数据类型

时间:2011-12-05 09:46:33

标签: c++ memory casting

我已经知道结构的指针可以转换为具有等效内存布局的结构的另一个指针。

但是,在下面的代码中:

#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

#define MYPORT "3490"  // the port users will be connecting to
#define BACKLOG 10     // how many pending connections queue will hold

int main(void)
{
    struct sockaddr_storage their_addr;
    socklen_t addr_size;
    struct addrinfo hints, *res;
    int sockfd, new_fd;

    // !! don't forget your error checking for these calls !!

    // first, load up address structs with getaddrinfo():

    memset(&hints, 0, sizeof hints);
    hints.ai_family = AF_UNSPEC;  // use IPv4 or IPv6, whichever
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = AI_PASSIVE;     // fill in my IP for me

    getaddrinfo(NULL, MYPORT, &hints, &res);

    // make a socket, bind it, and listen on it:

    sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
    bind(sockfd, res->ai_addr, res->ai_addrlen);
    listen(sockfd, BACKLOG);

    // now accept an incoming connection:

    addr_size = sizeof their_addr;
    new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &addr_size);

    // ready to communicate on socket descriptor new_fd!
    .
    .
    .

struct sockaddr_storage their_addr投放到(struct sockaddr *)&their_addr。每个结构的布局是:

struct sockaddr {
    unsigned short    sa_family;    // address family, AF_xxx
    char              sa_data[14];  // 14 bytes of protocol address
}; 

struct sockaddr_storage {
    sa_family_t  ss_family;     // address family

    // all this is padding, implementation specific, ignore it:
    char      __ss_pad1[_SS_PAD1SIZE];
    int64_t   __ss_align;
    char      __ss_pad2[_SS_PAD2SIZE];
};

socktaddr_storage绝对是一个更大的信息存储,所以它可以被转换为小于它的任何东西以适合函数声明。在函数内部,无论什么类型的结构都是无关紧要的,只要传递给函数的结构具有足够的内存块以使函数作为所需的结构运行。这是对的吗?

1 个答案:

答案 0 :(得分:3)

是的,这是正确的。基本上,它类似于将派生类指针转换为基类指针(实际上就是它在C中实现的方式)。

将根据ss_family的值设置14个字节的内容,然后您可以将其转换为one of the specific structs