Skip to content

Instantly share code, notes, and snippets.

@davestevens
Created January 12, 2012 16:09
Show Gist options
  • Save davestevens/1601347 to your computer and use it in GitHub Desktop.
Save davestevens/1601347 to your computer and use it in GitHub Desktop.
broken code gen
/*
This is a stripped down piece of code from a code generator.
It works in GCC but with another compiler I'm using is fails, its a real corner case though.
If there was only one struct (structure) then it wouldn't be a problem, when there is more than one it messes up.
The fact that the static structures foo and bar are defined and then redefined causes a problem in the assembly output. GCC doesn't seem to mind this and the function pointer is correct. The VEX assembly will output 2 memory areas with the same tag, one empty and one with the correct data.
Question: what should be the proper behaviour of this? The code is technically redefining the structures, but GCC gives no compiler warnings? Are structs different for defining?
Or would you say this is bad coding?
*/
typedef struct structure {
void (*send)(void);
} structure;
struct argument{
structure *structP;
};
static void thisIsAFunctionDefinition(void);
static structure foo;
static void function0(struct argument * const self) {
self->structP = &foo;
}
static structure bar;
static void function1(struct argument * const self) {
self->structP = &bar;
}
static structure foo = {
thisIsAFunctionDefinition
};
static structure bar = {
thisIsAFunctionDefinition
};
@iamscottmoyers
Copy link

After being confused, I think it's valid. The first instance of foo is a declaration and says that foo might be defined later in this file. The second foo is the actual definition. If the second foo didn't exist then the first foo would be declared by the compiler and set equal to 0.

You can have as many 'structure foo;'s as you want but you can only have a maximum of one with an initializer.

http://en.wikipedia.org/wiki/Forward_declaration

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment