我有一个名为 command 的结构,看起来像这样。枚举中的第一个值是 AND_COMMAND
struct command
{
enum command_type type;
int status;
char *input;
char *output;
union
{
struct command *command[2];
char **word;
struct command *subshell_command;
} u;
};
当我调用 pthread_create 时,我以 command_t 的形式向它传递一个命令(即强制转换为 (void *))。
typedef struct command *command_t;
我的线程采用这个(void *)command_t,将其转换回(command_t)并尝试使用该结构。
void execute_thread(void *c) {
command_t command = (command_t) c;
但是,当我将结构传递给 execute_thread 时,第一个值被清零。如果我有一个 SIMPLE_COMMAND 的 command_type 和 -1 的状态,当它被传递到线程时 command_type 是 AND_COMMAND 和 0 的状态。但是,结构中的其他值都没有改变。更奇怪的是何时发生这种数据修改。我能够在 gdb 中捕捉到这种现象:
445 command_t command = (command_t) c;
(gdb) p *((command_t) c)
$6 = {type = SIMPLE_COMMAND, status = -1, input = 0x605370 "abc", output = 0x605390 "def", u = {
command = {0x6052e0, 0x0}, word = 0x6052e0, subshell_command = 0x6052e0}}
(gdb) n
(gdb) p *((command_t) c)
$7 = {type = AND_COMMAND, status = 0, input = 0x605370 "abc", output = 0x605390 "def", u = {command = {
0x6052e0, 0x0}, word = 0x6052e0, subshell_command = 0x6052e0}}
似乎 c 指向的结构在将其转换之前不会改变,(command_t) c;
我完全被这种行为弄糊涂了。我不认为投射指针会改变它指向的值。有人可以指出,哈哈,这里可能发生了什么?我会非常感激。