铸造指针较大结构的指针,以更小的结构 [英] Casting pointers to larger structs to pointers to smaller structs

查看:95
本文介绍了铸造指针较大结构的指针,以更小的结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个职位将建设掉<一个href=\"http://stackoverflow.com/questions/16010622/reasoning-behind-c-sockets-sockaddr-and-sockaddr-storage/16010670\">this 之一,但将是一个小更通用。

This post will be building off of this one but will be a little more generic.

我经常看到指向大结构被强制转换为指针,以更小的结构,我的数字,因为工作的指针在结构中的第一个字节的只是地址。我仍然有一个问题的是如何大多数方法处理时,他们没有在他们所期望的类型指向指针。

Often I see pointers to large structs being cast to pointers to smaller structs and I figure that works because the pointers are just the address of the first byte in the structure. What I still have a question about is how to most methods handle pointers when they aren't pointing at the type they expect.

我将使用Socket API作为一个例子:

I will use the Socket API as an example:

sockaddr_storage 较大的sockaddr 而是指向 sockaddr_storage 获取转换为指针的sockaddr 它们传递给功能,如

sockaddr_storage is larger than sockaddr but pointers to sockaddr_storage get cast to pointers to sockaddr before they are passed to functions like

int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);

因此​​,如何职能往往处理事情时,指针指向一个更大的结构比预期的。难道他们说

So how to functions tend to handle things when the pointer is pointing to a larger structure than expected. Do they say

噢大小( socklen_t的addrlen中)传给我大于我认为这将是,更好的施放此指针回 sockaddr_storage

"Oh the size (socklen_t addrlen) passed to me is larger than I thought it would be. Better cast this pointer back to a sockaddr_storage!"

,然后访问其成员这样呢?或者他们做一些更复杂?

And then access its members like that? Or do they do something more complex?

推荐答案

的sockaddr 是C的可怜人多态成语的一个很好的例子。在面向对象语言中,的sockaddr 将是一个基类,从中协议地址类型( SOCKADDR_IN 的众人,体sockaddr_in6 sockaddr_ll 等)的派生。

sockaddr is a perfect example of C's poor-man polymorphism idiom. In an OO language, sockaddr would be a base class, from which the multitudes of protocol address types (sockaddr_in, sockaddr_in6, sockaddr_ll, etc.) are 'derived'.

基本上,一个的sockaddr 被定义为一个类型(上sa_family ),指定什么是真正成立,随后由一些数据。一个指向的sockaddr 通常指向派生结构( SOCKADDR _ * )的指定特定间pretation数据的。

Basically, a sockaddr is defined as having a type (sa_family) that specifies what it really holds, followed by some data. A pointer to a sockaddr usually points to a derived struct (sockaddr_*) which specifies a specific interpretation of the data.

sockaddr_storage 类似于普通的的sockaddr ,因为它不持有特定协议地址,只存储空间。不同的是, sockaddr_storage 指定更多的空间比的sockaddr ,使其成为一个合适的类型藏匿协议特定的sockaddr S IN

sockaddr_storage is similar to plain sockaddr, in that it doesn't hold a specific protocol address, only storage space. The difference is that sockaddr_storage specifies more space than sockaddr, making it a suitable type to stash the protocol-specific sockaddrs in.

一个函数在看一个普通的的sockaddr 首先查看上sa_family ,这是如 AF_INET (对于IPv4)或 AF_INET6 (对于IPv6)或别的东西,然后将其转换给予了指针相应的的sockaddr 亚型(如 SOCKADDR_IN 对于IPv4)。然后,它可以检查 addrlen中来确保铸造指针指向足够的空间容纳子类型。

A function looking at a plain sockaddr first looks at sa_family, which is e.g. AF_INET (for IPv4) or AF_INET6 (for IPv6) or something else, and then casts the pointer it was given to the appropriate sockaddr subtype (e.g. sockaddr_in for IPv4). It may then check addrlen to make sure the casted pointer points to enough space to hold the subtype.

这篇关于铸造指针较大结构的指针,以更小的结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆