Re: typedefs and structs

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tue, 8 Nov 2005 20:51:25 -0500, Kyle Moffett <[email protected]> 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))

Attachment: signature.asc
Description: PGP signature


[Index of Archives]     [Kernel Newbies]     [Netfilter]     [Bugtraq]     [Photo]     [Stuff]     [Gimp]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Video 4 Linux]     [Linux for the blind]     [Linux Resources]
  Powered by Linux