相关疑难解决方法(0)

永远不会执行的代码可以调用未定义的行为吗?

调用未定义行为的代码(在此示例中,除以零)将永远不会执行,程序是否仍未定义行为?

int main(void)
{
    int i;
    if(0)
    {
        i = 1/0;
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我认为它仍然是未定义的行为,但我在标准中找不到支持或否认我的任何证据.

那么,有什么想法吗?

c language-lawyer

79
推荐指数
3
解决办法
4316
查看次数

了解Linux内核中的container_of宏

当我浏览Linux内核时,我找到了一个container_of定义如下的宏:

#define container_of(ptr, type, member) ({                      \
        const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
        (type *)( (char *)__mptr - offsetof(type,member) );})
Run Code Online (Sandbox Code Playgroud)

我理解container_of做了什么,但我不明白的是最后一句,也就是说

(type *)( (char *)__mptr - offsetof(type,member) );})
Run Code Online (Sandbox Code Playgroud)

如果我们使用宏如下:

container_of(dev, struct wifi_device, dev);
Run Code Online (Sandbox Code Playgroud)

最后一句的相应部分是:

(struct wifi_device *)( (char *)__mptr - offset(struct wifi_device, dev);
Run Code Online (Sandbox Code Playgroud)

看起来什么都不做.有人可以在这里填补空白吗?

c linux-kernel c-preprocessor

71
推荐指数
5
解决办法
7万
查看次数

标签 统计

c ×2

c-preprocessor ×1

language-lawyer ×1

linux-kernel ×1