All of lore.kernel.org
 help / color / mirror / Atom feed
* Question with "container_of(ptr, type, member)"
@ 2011-08-30 10:44 peter
  2011-08-30 23:14 ` Ryan Mallon
  0 siblings, 1 reply; 3+ messages in thread
From: peter @ 2011-08-30 10:44 UTC (permalink / raw)
  To: linux-kernel

I have a question about the macro " container_of(ptr, type, member) "
I can write it as this,
 #define container_of(ptr, type, member) ({ \
	(type *) ((char *) ptr - offset_of(type, member)); \
	})
It can act the same as 
#define container_of(ptr, type, member) ({            \
	 const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
	 (type *)( (char *)__mptr - offsetof(type,member) );})
So why we don't use the first one ?
Thanks for your answer.
(I am a kernel newbie ,and sorry for my poor english~)



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Question with "container_of(ptr, type, member)"
  2011-08-30 10:44 Question with "container_of(ptr, type, member)" peter
@ 2011-08-30 23:14 ` Ryan Mallon
  2011-08-31  0:52   ` peter
  0 siblings, 1 reply; 3+ messages in thread
From: Ryan Mallon @ 2011-08-30 23:14 UTC (permalink / raw)
  To: peter; +Cc: linux-kernel

On 30/08/11 20:44, peter wrote:
> I have a question about the macro " container_of(ptr, type, member) "
> I can write it as this,
>   #define container_of(ptr, type, member) ({ \
> 	(type *) ((char *) ptr - offset_of(type, member)); \
> 	})
> It can act the same as
> #define container_of(ptr, type, member) ({            \
> 	 const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
> 	 (type *)( (char *)__mptr - offsetof(type,member) );})
> So why we don't use the first one ?
> Thanks for your answer.
> (I am a kernel newbie ,and sorry for my poor english~)

The version used by the Linux kernel does type checking.

~Ryan


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Question with "container_of(ptr, type, member)"
  2011-08-30 23:14 ` Ryan Mallon
@ 2011-08-31  0:52   ` peter
  0 siblings, 0 replies; 3+ messages in thread
From: peter @ 2011-08-31  0:52 UTC (permalink / raw)
  To: Ryan Mallon; +Cc: linux-kernel

I see now, It can gives an waring:  "warning: initialization from
incompatible pointer type", when give it an wrong type.
thanks for your answer!
On Wed, 2011-08-31 at 09:14 +1000, Ryan Mallon wrote:
> On 30/08/11 20:44, peter wrote:
> > I have a question about the macro " container_of(ptr, type, member) "
> > I can write it as this,
> >   #define container_of(ptr, type, member) ({ \
> > 	(type *) ((char *) ptr - offset_of(type, member)); \
> > 	})
> > It can act the same as
> > #define container_of(ptr, type, member) ({            \
> > 	 const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
> > 	 (type *)( (char *)__mptr - offsetof(type,member) );})
> > So why we don't use the first one ?
> > Thanks for your answer.
> > (I am a kernel newbie ,and sorry for my poor english~)
> 
> The version used by the Linux kernel does type checking.
> 
> ~Ryan
> 



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2011-08-31  0:52 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-08-30 10:44 Question with "container_of(ptr, type, member)" peter
2011-08-30 23:14 ` Ryan Mallon
2011-08-31  0:52   ` peter

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.