Re: unhappy with current.h

Daniele Lugli (genlogic@inrete.it)
Mon, 14 Oct 2002 23:22:26 +0200


"Richard B. Johnson" wrote:
>
> On Mon, 14 Oct 2002, Daniele Lugli wrote:
>
> > I recently wrote a kernel module which gave me some mysterious problems.
> > After too many days spent in blood, sweat and tears, I found the cause:
> >
> > *** one of my data structures has a field named 'current'. ***
> >
> > Pretty common word, isn't it? Would you think it can cause such a
> > trouble? But in some of my files I happen to indirectly include
> > <asm/current.h> (kernel 2.4.18 for i386), containing the following line:
> >
> > #define current get_current()
> >
> > so that my structure becomes the owner of a function it has never asked
> > for, while it looses a data member. gcc has nothing to complain about
> > that.
> >
>
> This cannot be the reason for your problem. The name of a structure
> member has no connection whatsoever with the name of any function or
> definition.
>
> The following code will correctly write "Hello world!" to the screen
> even though the text initializes a member of a structure called "current"
> while "current" has been defined to be a function called puts.
>
> #include <stdio.h>
> #define current puts
> struct foo {
> char *current;
> int foo;
> } bar;
> main()
> {
> bar.current = "Hello world!";
> current(bar.current);
> return 0;
> }
>
> For your code to get "confused", you really have something else
> wrong. That said, some name-space polution may make it difficult
> to find the problem. For instance, a structure member is expected
> to have a ";" after it. It's possible for some previous definition
> to make a syntax error invisible.
>
> Cheers,
> Dick Johnson

Try the following instead:

// file shade1.cpp

// excerpt from common.h

#define current get_current()

// my stuff

struct {
int any;
int current[1000];
} mine;

// file shade2.cpp

#include <stdio.h>

// my stuff

extern struct {
int any;
int current[1000];
} mine;

int main () {
mine.current[999] = 1;
printf ("%d\n", mine.current[999]);
}

g++ shade1.cpp shade2.cpp

./a.out => segmentation fault

Regards, Daniele
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/