这是有效的ANSI C ++代码吗?尝试在编译时生成结构成员的偏移量 [英] Is this valid ANSI C++ code? Trying to generate offsets of structure members at compile-time

查看:93
本文介绍了这是有效的ANSI C ++代码吗?尝试在编译时生成结构成员的偏移量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复项:
执行< stddef.h>中的'offsetof'宏;调用未定义的行为?
取消引用空指针

Possible Duplicates:
Does the 'offsetof' macro from <stddef.h> invoke undefined behaviour?
dereferencing the null pointer

    #define _OFFS_OF_MEMBER(p_type, p_member) (size_t)&(((p_type *)NULL)->p_member)

    struct a 
    {
             int a, b;
    };

    size_t l = _OFFS_OF_MEMBER(struct a, b);

我与一些其他用户进行了一些聊天/交谈,其中一个用户说这是取消引用并访问地址NULL附近的地址空间.我说过:获取成员的地址将不会访问,触摸或读取该成员的值.根据标准,它是完全安全的.

I had a little chat/conversation with some fellow users, and one of them said that this is dereferencing and accessing the address space near address NULL. I said: taking an address of a member will not access, touch, or read the value of that member. According to standard it is completely safe.

    struct a* p = NULL;
    size_t offset = &p->b; // this may NOT touch b, it is not dereferencing
    // p->b = 0; // now, we are dereferincing: acccess violation time!

这始终是计算偏移量的安全方法,还是编译器可以根据标准自由地取消引用并弄乱地址NULL附近的内存?

Is this always a safe way to calculate offset, or are compilers free to dereference and mess up the memory near address NULL according to standards?

我知道有一种安全的方法可以计算标准所提供的偏移量,但是我很好奇您对此有何评论.所有人都赞成我的夸奖:投票赞成这个问题:-)

I know there is a safe way to calculate offsets provided by the standard, but I am curious what you have to say about this. All in favor of my explenation: up-vote this question :-)

推荐答案

您不会在这里取消引用任何无效的内容.宏所做的全部工作就是告诉编译器,地址为NULL的内存中存在类型为p_type的结构.然后,它使用p_member的地址,该地址是此虚拟结构的成员.因此,在任何地方都不会取消引用.

You're not dereferencing anything invalid here. All that macro does is tell the compiler that a structure of type p_type exists in memory at the address NULL. It then takes the address of p_member, which is a member of this fictitious structure. So, no dereferencing anywhere.

实际上,这正是 offsetof 宏的定义在stddef.h中.


正如某些评论所言,这可能不适用于C ++和继承,我只对C中的POD结构使用offsetof.


As some of the comments say, this may not work well with C++ and inheritance, I've only used offsetof with POD structures in C.

这篇关于这是有效的ANSI C ++代码吗?尝试在编译时生成结构成员的偏移量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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