开发者

Does GCC create typedefs for arrays passed to functions?

开发者 https://www.devze.com 2023-01-19 21:33 出处:网络
While debugging some C code with gdb I came across something I\'ve not seen nor heard of before! The compiler (gcc -O0) seems to have created a new type for passing an array of vectors to a function..

While debugging some C code with gdb I came across something I've not seen nor heard of before! The compiler (gcc -O0) seems to have created a new type for passing an array of vectors to a function... I think! Have a look at the code and gdb information below:

/* The Vector type - nothing unusual */
typedef struct {
    float x,y,z;
} Vector;

/* The function I was debugging.
 * The second parameter is what seems to have changed */
extern void gui_shader_draw(
    guiShader *shader,
    Vector quad[ 4 ], // << This is the one!
    Vector origin,
    Vector rotation,
    Vector scale,
    bool focus,
    int mode );

I set a breakpoint inside the gui_shader_draw function and this is what I see:

开发者_如何学编程
break shader.c:248
Breakpoint 1 at 0x80013ac0: file src/gui/shader.c, line 248.
(gdb) continue
Continuing.
// I have split the next line
Breakpoint 1, gui_shader_draw (
    shader=0x80588ea8,
    quad_t=0x80585fe8, // << What the?
    origin=...,
    rotation=...,
    scale=...,
    focus=false,
    mode=3) at src/gui/shader.c:249

// The values quad_t points to are all good
(gdb) print quad_t[0]
$10 = {x = -320, y = -240, z = 0}
(gdb) print quad_t[1]
$11 = {x = 320, y = -240, z = 0}
(gdb) print quad_t[2]
$12 = {x = 320, y = 240, z = 0}
(gdb) print quad_t[3]
$13 = {x = -320, y = 240, z = 0}

Where did quad_t come from? It's certainly not a typedef in any of my code. The system header sys/types.h has a quad_t alias (long int) but that doesn't seem at all related! What's going on? Have I missed something obvious?

EDIT 1: I must point out that the code compiles and works fine. There are no clashes with some other variable or type called quad_t. I am merely curious about what GCC has done and why.

EDIT 2: As suggested, I had a look over the preprocessor output and indeed all instances of 'Vector quad[4]' have been changed to 'Vector quad_t[4]', so the name has changed, not the type.

extern void gui_shader_draw(
    guiShader *shader,
    Vector quad_t[ 4 ],
    Vector origin,
    Vector rotation,
    Vector scale,
    _Bool focus,
    int mode
 );

There are no typedefs called 'quad_t' in the preprocessor output though. But I did find this in sys/types.h (which I missed before - d'oh!)

/usr/include$ find . | xargs grep -s quad_t
./sys/types.h:typedef    __uint64_t      u_quad_t;
./sys/types.h:typedef    __int64_t       quad_t;
./sys/types.h:typedef    quad_t *        qaddr_t;
./sys/types.h:#  define  quad            quad_t // << There you are!


What happens in your code has absolutely nothing to do with any typedefs, since the change is in no way related to any types at all. What has changes is the name of function parameter not its type, which is immediately obvious from your debugger output.

Since you see that change in the preprocessor output, the only reasonable explanation is that somewhere in the code there's a

#define quad quad_t

So you have to look for #defines for quad, not for quad_t. Needless to say, that #define will not be present in preprocessor output. You have to do a global search in all included (both directly and indirectly) header files.

0

精彩评论

暂无评论...
验证码 换一张
取 消