socklen_t 类型

 在Linux下编译程序,accept( ; ; ;) 函数的第三个参数提示“从类型‘int*’到类型‘socklen_t*’的转换无效”,于是乎,我发现还有个 socklen_t 类型。
解决办法: 
    将保存 struct sockaddr_un 结构的长度的变量类型,由 int 类型改为 socklen_t 类型。

    将 int 类型改为 socklen_t 类型之后,编译含有 accept( ; ; ;)  的代码,顺利通过编译。

 

话说,看到这么一个宏,也可以计算 struct sockaddr_un 结构的长度。
    int size = offsetof(struct sockaddr_un, sun_path) + strlen(un.sun_path); 
我在想,这样计算得到的 size 是不是就不需要 改 int 类型为 socklen_t 类型 了呢? (2012-06-11)
引文地址: http://blog.csdn.net/blueliuyun/article/details/7638532


如何在Linux下搜索 socklen_t 类型所在的文件呢
--> cd /usr/include 
--> grep -r socklen_t * | grep typedef

得到的结果截图如下:


参考链接:
http://blog.csdn.net/yanyiyyy/article/details/6642867


http://blog.csdn.net/thomas_nuaa/article/details/3542572

C语言中offset的使用

offsetof  :
    Retrieves the offset of a member from the beginning of its parent structure.

size_t offsetof(structName, memberName);

Parameters:
    structName : Name of the parent data structure.
    memberName :Name of the member in the parent data structure for which to determine the offset.

Return Value : offsetof returns the offset in bytes of the specified member from 
          the beginning of its parent data structure. It is undefined for bit fields.
Remarks :
    The offsetof macro returns the offset in bytes of memberName from the beginning of the structure specified by structName. You can specify types with the struct keyword.

Note  :
    offsetof is not a function and cannot be described using a C prototype.

 

 #define offsetof(s, m)   (size_t)&(((s *)0)->m)

s是一个结构名,它有一个名为m的成员(s和m 是宏offsetof的形参,它实际是返回结构s的成员m的偏移地址.

(s *)0 是骗编译器说有一个指向类(或结构)s的指针,其地址值0 

&((s *)0)->m   是要取得类s中成员变量m的地址. 因基址为0,这时m的地址当然就是m在s中的偏移

最后转换size_t 型,即unsigned int。

有例子如: 
struct   A 

    int   i; 
    int   j; 
}; 
   
struct   A   *pA; 
pA = new   A; 
这时,pA实际上是一个Pointer, 指向某一确定的内存地址, 如0x1234; 
而pA->i 整体是一个int型变量,其地址是&(pA->i), '&'为取址运算符; 
那么&(pA->i)一定等于0x1234,因 i 是结构体A的第一个元素。 
而&(pA->j)一定是0x1234 + 0x4 = 0x1238; 因为sizeof(int) = 4; 
   
这个做法的巧妙之处就是:它把“0”作为上例中的pA,那么&(pA->j)就是 j 的offset 

解析结果是: 
(s*)0,将 0 强制转换为Pointer to "s"    
可以记 pS = (s*)0,pS是指向s的指针,它的值是0; 
那么pS->m就是m这个元素了,而&(pS->m)就是m的地址,就是offset

下面是个offsetof应用的例子,其中宏OBJECT_HEAD_ADDRES的作用是根据一个对象或结构的某成员的地址,求其首地址。

 

完整的例子如下:

/* offsetof example */

#include "stdafx.h"
#include <stdio.h>
#include <stddef.h>

/************************************************************************/
/* Macro OBJECT_HEAD_ADDRESS : calculate the struct's address according
/* to one of its member's address
/************************************************************************/
#define OBJECT_HEAD_ADDRESS(ClassName,MemberName,Addre) /
Addre - offsetof(ClassName, MemberName)

struct S
{
int ID;
char *addr;
char name[20];
};

struct S1
{
S employee;
char *title;
};

struct S2
{
char singlechar;
int arraymember[10];
char anotherchar;
};

int main(int argc, char* argv[])
{
/* Example 1 
S2 s2 = {'a', 10, 'b'};
int head = int(&(s2.singlechar));
int memb = int(&(s2.anotherchar));

int i = OBJECT_HEAD_ADDRESS(S2, anotherchar, memb);

printf ("offsetof(S2, singlechar) is %d/n", offsetof(S2, singlechar));
printf ("offsetof(S2, arraymember) is %d/n", offsetof(S2, arraymember));
printf ("offsetof(S2, anotherchar) is %d/n", offsetof(S2, anotherchar));
printf("s2.anotherchar's address is %x ==> s2's address is %x/n", memb, i);
*/
/* Example 2 
S s = {100, "Nanjing", "Thomas"};
int id = int(&(s.ID));
int ad = int(&(s.addr));
int nm = int(&(s.name));

int j = OBJECT_HEAD_ADDRESS(S, addr, ad);

printf ("offsetof(S, ID) is %d/n", offsetof(S, ID));
printf ("offsetof(S, addr) is %d/n", offsetof(S, addr));
printf ("offsetof(S, name) is %d/n", offsetof(S, name));
printf ("s.ID's address : %x/ns.addr's address : %x/ns.name's address : %x/n", id, ad, nm);
printf("s.addr's address is %x ==> s's address is %x/n", ad, j);
*/
/* Example 3 */
S1 s1 = {{100, "Nanjing", "Thomas Chen"},"Thomas Chen"};
int td = int(&(s1.title));
int k = OBJECT_HEAD_ADDRESS(S1, title, td);

printf ("offsetof(S1, employee) is %d/n", offsetof(S1, employee));
printf ("offsetof(S1, title) is %d/n", offsetof(S1, title));
printf("s1.title's address is %x ==> s1's address is %x/n", td, k);

return 0;
}
/* Example 1 Output */
offsetof(S2, singlechar) is 0
offsetof(S2, arraymember) is 4
offsetof(S2, anotherchar) is 44
s2.anotherchar's address is 12ff7c ==> s2's address is 12ff50

/* Example 2 Output */
offsetof(S, ID) is 0
offsetof(S, addr) is 4
offsetof(S, name) is 8
s.ID's address : 12ff64
s.addr's address : 12ff68
s.name's address : 12ff6c
s.addr's address is 12ff68 ==> s's address is 12ff64

/* Example 3 Output */
offsetof(S1, employee) is 0
offsetof(S1, title) is 28
s1.title's address is 12ff7c ==> s1's address is 12ff60


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值