简体   繁体   English

将结构作为指针传递,导致数组损坏?

[英]Passing a struct as a pointer, resulting in a corrupted array?

I have a struct being passed in as a void* pointer 我有一个传递为void *指针的结构

void *find_queens(void *args) {  

I tried to turn this pointer in a usable struct using this 我试图使用此将指针变为可用的结构

struct queens_arg *data = (struct queens_arg *) args;

However, the array that is stored within this 但是,存储在其中的数组

struct queens_arg {
  int board[64]; 
  int focus_idx;
};

called board is now being corrupted and does not reflect the original values, does anyone knows why? 所谓的董事会现在已损坏,不能反映原始值,有人知道为什么吗? Thanks! 谢谢!

More information here: 此处有更多信息:

This is the start of the function: 这是该功能的开始:

void *find_queens(void *args) {  

  //vars
  pthread_t thread1, thread2;
  struct queens_arg *data = (struct queens_arg *) args;
  int board[64];
  copy_array(data->board, board);
  print_board(data->board);

This is how it is called: 这就是所谓的:

int board[64] = {
    0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,
  };

  struct queens_arg *args = malloc(sizeof (struct queens_arg));
  args->focus_idx = 0;
  copy_array(board,args->board);
  (*find_queens)(&args);

When I print the array, I get this instead: 当我打印数组时,我得到的是:

39456784 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0

Instead of 0 all the way. 一路代替0。 Which is weird. 这很奇怪。

I think that the problem is that what you're passing in to the function is a struct queens_arg** , not a struct queens_arg* . 我认为问题在于传递给函数的是struct queens_arg** ,而不是struct queens_arg* Notice that you're passing in a pointer to the pointer here: 注意,您在此处传递了一个指向该指针的指针:

(*find_queens)(&args);

When you try typecasting it here: 当您尝试在此处进行类型转换时:

struct queens_arg *data = (struct queens_arg *) args;

You're converting a struct queens_arg** to a struct queens_arg* , which isn't a meaningful conversion. 您正在将struct queens_arg**转换为struct queens_arg* ,这不是有意义的转换。 You'll end up reading data near the pointer as though it were a struct queens_arg , which isn't what you want. 您最终将读取指针附近的数据,就像它是一个struct queens_arg ,这不是您想要的。

To fix this, just pass in the args pointer by itself, rather than a pointer to the args pointer: 要解决此问题,只需args传递args指针,而不要传递指向args指针的指针:

(*find_queens)(args);

Out of curiosity, is there a reason that you're taking in a void* instead of a struct queens_arg* ? 出于好奇,您是否有理由要使用void*而不是struct queens_arg* Part of the problem is that the compiler can't diagnose the nonsensical cast because you're funneling everything through void* first. 问题的一部分是编译器无法诊断无意义的转换,因为您首先要通过void*所有漏斗处理。

Casting a pointer to a struct to a *void and back is perfectly legal even according to the C standard, to that is unlikely to be the problem. 即使根据C标准,将指向结构的指针强制转换为* void并返回也是完全合法的,这不太可能成为问题。 Are you certain the pointer really starts out as a pointer to your struct queens_arg? 您确定指针实际上是作为指向结构Queens_arg的指针开始的吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM