[lkml]   [2005]   [Nov]   [9]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
Messages in this thread
    SubjectRe: typedefs and structs
    On Wed, 9 Nov 2005, J.A. Magallon wrote:

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

    A diligent C programmer would write this as follows:
    void do_some_stuff (struct T * a, struct T * b);
    void do_more_stuff (const struct T * a, const struct T * b);
    So I don't see C++ winning at all here.

    > 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.

    I think I'll call this my rule #1:
    The moment you stop worrying about something is the moment it bites you
    in the butt. :-) Much firsthand experience.

    > The person who programs the function decides and can even change it without
    > you user even noticing.

    And if the caller is passing in something that's not meant to be
    modified, then the modification causes much badness. Happens with both
    languages, too.

    > And gcc does nice optimizations when you mix const& and inlining...

    As far as I know, nothing stops GCC from doing the exact same
    optimizations in the function prototypes given above.

    > --
    > 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))

    -Vadim Lobanov
    To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
    the body of a message to
    More majordomo info at
    Please read the FAQ at

     \ /
      Last update: 2005-11-09 17:24    [W:0.025 / U:12.280 seconds]
    ©2003-2017 Jasper Spaans. hosted at Digital OceanAdvertise on this site