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:
在用gdb调试一些C代码时,我遇到了一些我以前从未见过或听过的东西!编译器(gcc -O0)似乎创建了一个新类型,用于将向量数组传递给函数......我想!看看下面的代码和gdb信息:
/* 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:
我在gui_shader_draw函数中设置了一个断点,这就是我所看到的:
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?
quad_t来自哪里?它在我的任何代码中肯定不是typedef。系统头sys / types.h有一个quad_t别名(long int),但似乎没有任何关系!这是怎么回事?我错过了一些明显的东西吗
EDIT 1: I must point out that the code compiles and works fine. There are no *es with some other variable or type called quad_t. I am merely curious about what GCC has done and why.
编辑1:我必须指出代码编译并正常工作。与其他一些名为quad_t的变量或类型没有冲突。我只是对海湾合作委员会所做的事情及其原因感到好奇。
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.
编辑2:正如所建议的那样,我查看了预处理器输出,实际上'Vector quad [4]'的所有实例都已更改为'Vector quad_t [4]',因此名称已更改,而不是类型。
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!)
但是在预处理器输出中没有名为“quad_t”的typedef。但我确实在sys / types.h中找到了这个(我以前错过了 - 哦哦!)
/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!
1 个解决方案
#1
4
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.
您的代码中发生的事情与任何typedef完全无关,因为更改根本不与任何类型相关。更改的是函数参数的名称而不是其类型,这在调试器输出中很明显。
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 #define
s 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.
所以你必须为quad而不是quad_t寻找#defines。不用说,#define不会出现在预处理器输出中。您必须在所有包含(直接和间接)头文件中进行全局搜索。
#1
4
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.
您的代码中发生的事情与任何typedef完全无关,因为更改根本不与任何类型相关。更改的是函数参数的名称而不是其类型,这在调试器输出中很明显。
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 #define
s 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.
所以你必须为quad而不是quad_t寻找#defines。不用说,#define不会出现在预处理器输出中。您必须在所有包含(直接和间接)头文件中进行全局搜索。