我的问题如下 - 我有这行代码:
// allocate a new vector
Vector3I *theVector = (Vector3I*)calloc(1,sizeof(Vector3I));
// write face's points to this vector
*theVector[0] = a3dsFace->points[0];
*theVector[1] = a3dsFace->points[1];
*theVector[2] = a3dsFace->points[2];
points[]
数组中的值为{0,1,2}
。当我将它们写到我指向的Vector3I
时,我得到{0,0,0}
。你对我做错了什么有任何建议吗?
编辑:更多细节:
这是来自lib3ds:http://code.google.com/p/lib3ds/
struct Lib3dsFace {
Lib3dsUserData user; /*! Arbitrary user data */
char material[64]; /*! Material name */
Lib3dsWord points[3]; /*! Indices into mesh points list */
Lib3dsWord flags; /*! See Lib3dsFaceFlag, below */
Lib3dsDword smoothing; /*! Bitmask; each bit identifies a group */
Lib3dsVector normal;
};
a3dsFace
是Lib3dsFace
结构。
点数组来自这种类型:
typedef unsigned __int16 Lib3dsWord
我的指针:
Vector3I* theVector
是
typedef int Vector3I[3];
我希望这会为问题带来一些启示。
亲切的问候。
答案 0 :(得分:2)
试试这个(注意分配3个元素并使用数组表示法访问):
Vectore3I *theVector = (Vector3I*)calloc(3, sizeof(Vector3I));
theVector[0] = a3dsFace->points[0];
theVector[1] = a3dsFace->points[1];
theVector[2] = a3dsFace->points[2];
答案 1 :(得分:2)
以下代码 工作,是对您的代码段的测试。如果某些内容无效,最好使用*a3dsFace
的硬编码值创建此类测试,以缩小问题范围。
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
typedef int Vector3I[3];
typedef uint16_t Lib3dsWord;
struct Lib3dsFace {
/* ... */
Lib3dsWord points[3]; /*! Indices into mesh points list */
/* ... */
};
/* ... */
struct Lib3dsFace some_face = { {0, 1, 2} };
struct Lib3dsFace *a3dsFace = &some_face;
/* ... */
int main(void)
{
Vector3I *theVector = (Vector3I*)calloc(1,sizeof(Vector3I));
(*theVector)[0] = a3dsFace->points[0];
(*theVector)[1] = a3dsFace->points[1];
(*theVector)[2] = a3dsFace->points[2];
printf("theVector: %p, *theVector: %p, &(*theVector)[0]: %p\n", theVector, *theVector, &(*theVector)[0]);
printf("RIGHT Addresses: %p, %p, %p\n", &(*theVector)[0], &(*theVector)[1], &(*theVector)[2]);
printf("WRONG Addresses: %p, %p, %p\n", &*theVector[0], &*theVector[1], &*theVector[2]);
printf("Values: %d, %d, %d\n", (*theVector)[0], (*theVector)[1], (*theVector)[2]);
free(theVector);
return 0;
}
输出:
theVector: 0x1cd3010, *theVector: 0x1cd3010, &(*theVector)[0]: 0x1cd3010
RIGHT Addresses: 0x1cd3010, 0x1cd3014, 0x1cd3018
WRONG Addresses: 0x1cd3010, 0x1cd301c, 0x1cd3028
Values: 0, 1, 2
我将地址放在那里,以便您可以看到(*theVector)[0]
是访问动态分配的Vector3I
的第一个元素的有效方式。
也许您没有正确设置a3dsFace->points
,这就是复制{0, 0, 0}
的原因。另请注意,您将Vector3I
的每个元素都设置为int
类型,并且每个点都是uint16_t
类型。您也不需要使用calloc
将已分配的内存归零,因为您在为它们分配值后立即执行此操作;你可以使用malloc
。
最重要的是,您仍然没有提供足够的代码来查找确切的问题,您应该添加代码来调试代码中的代码。
编辑:我不小心有*theVector[0]
本来应该(*theVector)[0]
,因为[]
的优先级高于*
。否则它会导致未定义的行为,因为你要经过数组的边界,我的不好。我不知道我是怎么忘记的,因为这是我在你编辑之前发布答案的主要原因之一。它工作正常,但如果你通过像valgrind这样的程序运行它会告诉你某些东西不太正确(即使它可能按预期运行)。
正如您在上面输出的地址所看到的那样,存在很大差异。例如,*theVector[1]
由于运算符优先级与*(theVector[1])
相同,将意味着它将theVector
所指向的地址增加3 * sizeof(int)
个字节(又名sizeof(Vector3I)
),而不只是1 * sizeof(int)
中(*theVector)[1]
的{{1}}。{/ p>
答案 2 :(得分:1)
这个怎么样:
// allocate a new vector
Vector3I *theVector = calloc(1,sizeof(Vector3I));
// write face's points to this vector
(*theVector)[0] = a3dsFace->points[0];
(*theVector)[1] = a3dsFace->points[1];
(*theVector)[2] = a3dsFace->points[2];