typedefs and structs

J.A. Magallon jamagallon at able.es
Wed Nov 9 21:16:40 EST 2005


On Tue, 8 Nov 2005 20:51:25 -0500, Kyle Moffett <mrmacman_g4 at mac.com> wrote:

> 
> Pass by value in C:
> do_some_stuff(arg1, arg2);
> 
> Pass by reference in C:
> do_some_stuff(&arg1, &arg2);
> 
> This is very obvious what it does.  The compiler does type-checks to  
> make sure you don't get it wrong.  There are tools to check stack  
> usage of functions too.  This is inherently obvious what the code  
> does without looking at a completely different file where the  
> function is defined.
> 
> 
> Pass by value in C++:
> do_some_stuff(arg1, arg2);
> 
> Pass by reference in C++:
> do_some_stuff(arg1, arg2);
> 
> This is C++ being clever and hiding stuff from the programmer, which  
> is Not Good(TM) for a kernel.  C++ may be an excellent language for  
> userspace programmers (I say "may" here because some disagree,  
> including myself), however, many of the features are extremely  
> problematic for a kernel.
> 

Why is it not good for kernel ?
You want to pass an struct to a function in the best way you can.
Reference just pases a pointer instead of copying, but you don't
realize.
If you want the funcion to be able to modify the struct, code it as

void do_some_stuff(T& arg1,T&  arg2)

If you DO NOT want the funcion to be able to modify the struct, code it as

void do_some_stuff(const T& arg1,const T& arg2)
This is far better than in C,. because you get the benefits from
reference pass without the problems of accidental modification of
pointer contents. And get rid of arrows -> ;).

If the function modifies the struct it should be obvious from its name,
not depending if you put an & in the call or not.
And you stop worrying about argument pass methods.
The person who programs the function decides and can even change it without
you user even noticing.
And gcc does nice optimizations when you mix const& and inlining...

--
J.A. Magallon <jamagallon()able!es>     \               Software is like sex:
werewolf!able!es                         \         It's better when it's free
Mandriva Linux release 2006.1 (Cooker) for i586
Linux 2.6.14-jam1 (gcc 4.0.2 (4.0.2-1mdk for Mandriva Linux release 2006.1))
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://ozlabs.org/pipermail/linuxppc64-dev/attachments/20051109/8e4a0d1f/attachment.pgp 


More information about the Linuxppc64-dev mailing list