Macro explanation

Mandeep Sandhu mandeepsandhu.chd at gmail.com
Mon Mar 12 00:40:06 EDT 2012


On Mon, Mar 12, 2012 at 1:03 AM, Vijay Chauhan <kernel.vijay at gmail.com> wrote:
> On Sat, Mar 10, 2012 at 12:10 AM, Manohar Vanga <manohar.vanga at gmail.com> wrote:
>> Also, from the
>> archives: http://www.mail-archive.com/kernelnewbies@nl.linux.org/msg12320.html
>
> Thank you for links which gives good explanation.
> But I am not able to understand the part accessing a member through
> NULL pointer like ((size_t) &((TYPE *)0)->MEMBER)
> How it is handled?

This macro is interpreted at 'compile-time', i.e during the
preprocessor stage...the NULL pointer exception would be generated if
the code was 'run' at runtime. The typeof GCC builtin is also
interpreted at compile-time. CMIIW. Hope that explains the seeming
NULL pointer dereference.

-mandeep

>
> In C code if we access a member through NULL pointer like this will
> cause the code to crash.
> Am I missing something?
>
>
>>
>> On Fri, Mar 9, 2012 at 7:21 PM, Vijay Chauhan <kernel.vijay at gmail.com>
>> wrote:
>>>
>>> Hi,
>>>
>>> I'm checking the container_of and offsetof macro
>>>
>>> #define container_of(ptr, type, member) ({ \
>>>                const typeof( ((type *)0)->member ) *__mptr = (ptr);
>>>                (type *)( (char *)__mptr - offsetof(type,member) );})
>>>
>>> #define offsetof(TYPE, MEMBER) \
>>>  ((size_t) &((TYPE *)0)->MEMBER)
>>>
>>>
>>> I did not understand the first line of both macro. Will it not create
>>> the NULL pointer dereference problem? I know it works and read some
>>> article but it did not explain this part, so can anyone explain why
>>> the NULL pointer error is not coming. Am I missing something?
>>> Any C language specification of such example.
>>>
>>> _______________________________________________
>>> Kernelnewbies mailing list
>>> Kernelnewbies at kernelnewbies.org
>>> http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
>>
>>
>>
>>
>> --
>> /manohar
>
> _______________________________________________
> Kernelnewbies mailing list
> Kernelnewbies at kernelnewbies.org
> http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies



More information about the Kernelnewbies mailing list