1

我需要帮助确定函数调用分配的正确数据类型。

我正在尝试获取content. N_Vector u以下是文档所说的N_Vector

类型N_Vector定义为

N_Vector u;
tpedef struct _generic_N_Vector *N_Vector;
struct _generic_N_Vector {
  void *content;
  struct _generic_N_Vector_Ops *ops;
};

...

[并行NVECTOR模块]将内容字段定义为N_Vector 包含全局和局部长度的结构、指向连续局部数据数组开头的指针、MPI通信器和标志。

struct _N_VectorContent_Parallel {
  long int local_length;
  long int global_length;
  booleantype own_data;
  realtype *data;
  MPI_Comm comm;
}

所以我想这意味着content_generic_N_Vector指向”一个类型的结构_N_VectorContent_Parallel(对吗?)。

然后我尝试使用宏来访问content. 这是NV_CONTENT_P.

v_cont=NV_CONTENT_P(v)设置v_cont为指向N_Vectortype 的内容结构的指针struct _N_VectorParallelContent

注意结构的不同名称!

这意味着什么?我声明v_cont是什么类型?

我试过了

N_Vector u;
...
_N_VectorParallelContent *v_cont1;
_N_VectorContent_Parallel *v_cont2;
v_cont1 = NV_CONTENT_P(u);
v_cont2 = NV_CONTENT_P(u);

但这些声明得到错误“'_N_VectorContent_Parallel' undeclared...”或“'_N_VectorParallelContent' undeclared...”。

但似乎这些结构必须已经被删除。我成功地声明(并使用了)u,类型为N_Vector。并且文档似乎说N_Vector包含这两种结构之一(或者可能两者兼有)。

那么为什么会出现错误消息? v_cont 声明接收数据 的正确数据类型是NV_CONTENT_P什么?

我知道这是一个冗长而详细的问题,但我理解的不够深入,无法再减少它。谢谢你的帮助。

4

1 回答 1

1

I'm not familiar with this particular library, but it looks to me like the documentation is a little inconsistent.

Right after the blurb about NV_CONTENT_P(v), it says NV_CONTENT_P(v) is defined as:

#define NV_CONTENT_P(v) ( (N_VectorContent_Parallel)(v->content) )

So that version of the name is probably correct. I can't see a definition for N_VectorContent_Parallel on that page, but it's probably defined somewhere as something like struct _N_VectorContent_Parallel*. So, you can probably do:

N_VectorContent_Parallel v_cont1 = NV_CONTENT_P(u);

Remember that for structs, struct is part of the type name. This means that you're getting errors in your example because you haven't included struct:

// this is an unknown type
_N_VectorParallelContent *v_cont1; 

// this is a "struct _N_VectorParallelContent"
struct _N_VectorParallelContent *v_cont1; 

// But use this one, as it follows the macro
N_VectorContent_Parallel v_cont1;

If you want to see exactly what the preprocessor has done to your code, you can use gcc's -E flag.

-E    Stop after the preprocessing stage; do not run the compiler proper. 
   The output is in  the form of preprocessed source code, which is sent to 
   the standard output.
   Input files which don't require preprocessing are ignored.

This is especially useful for seeing the results of macros and multiple complex header files.


Edit: From the source you've linked:

typedef struct _N_VectorContent_Parallel *N_VectorContent_Parallel;

This is a type definition that says that N_VectorContent_Parallel is the same as a struct _N_VectorContent_Parallel * (a pointer to a struct _N_VectorContent_Parallel), which means you can access v_cont1 using the -> syntax:

N_VectorContent_Parallel v_cont1;
printf("%d",v_cont1->local_length);

a->b is is shorthand for (*a).b - it's just a cleaner-looking way of writing the dereference needed to accessing a member of a struct through a pointer to that struct. If that seems confusing, see my answer to this question.

Personally, I don't like typedefs that hide pointers like this one, because it's hard to tell by looking at the code whether you need to use a.b or a->b.

于 2012-09-03T02:39:50.390 回答