c - 指针扰乱了我的价​​值观

标签 c pointers

我正在做这个学校项目,它画了两只兔子,你应该能看穿其中一只。请注意,整个代码只是老师制作的模板,我们应该用自己的代码填充它。 所以我有一个遍历所有像素的循环,而不是立即绘制,我们应该将它们存储在 Head 和 Node Buffer 中。 HeadBuffer 仅存储稍后在访问 NodeBuffer 时使用的索引。

请看这里:http://developer.amd.com/wordpress/media/2013/06/2041_final.pdf

我的一个问题是在填充缓冲区时无法在存储值后立即正确撤回该值,因此我无法正确访问它以对其进行排序、混合颜色和写出。

用C语言编写

声明:

//in student.c
local:
    int Q;
global:
    S_Frag fragmentIn;
    S_Frag *fragmentOut;
    int *ext;
    int index;
//up to here

.

typedef struct S_Vector
{
   int     size;           
   int     reserved;       
   int     elemSize;       
   char    * data;         
} S_Vector;

typedef struct S_Frag
{
    S_RGBA      color;      
    double      depth;      
    int         next;       
} S_Frag;

//returns pointer to the i-th element
IZG_INLINE void * vecGetPtr(S_Vector *pVec, int i)
{
    IZG_ASSERT(pVec && i < pVec->size);
    return pVec->data + i * pVec->elemSize;
}

IZG_INLINE S_Frag makeFrag(S_RGBA color, double depth, int next)
{
    S_Frag frag;
    frag.color = color;
    frag.depth = depth;
    frag.next = next;
    return frag;
}

//appends pElem to the end of S_Vector, returns index of new pElem
int vecPushBack(S_Vector *pVec, void *pElem);

//sets the i-th elem. of pVec to pElem
IZG_INLINE void vecSet(S_Vector *pVec, int i, void * pElem)
{
    IZG_ASSERT(pVec && i < pVec->size && pElem);
    MEMCOPY(pVec->data + i * pVec->elemSize, pElem, pVec->elemSize);
}

.

//student.c
Q = x + y*globWidth;  //make 2 dimensional array 1 dimensional

ext = (int *) vecGetPtr(HeadBuffer, Q); //get index that was in headBuffer

fragmentIn = makeFrag(color, z, *ext); //put it in a new fragment

index = vecPushBack(NodeBuffer, &fragmentIn);//put new frag. in nodebuffer

vecSet(HeadBuffer, Q, &index);//update index in headbuffer

//here comes the messed up part
fragmentOut = (S_Frag *) vecGetPtr(NodeBuffer, index);
printf("::%d\n", fragmentOut->next);

// expected: -1 or 435 or 46 or 1120
// gotten: -8503629141

当我让循环运行并像这样打印它时,得到的值也会发生变化,但仍然很大

Headbuffer 被初始化为保存 sizeof(int) 和 width*heigth(of the window),每个元素在开始时都是 -1

Nodebuffer 只是设置为保存 sizeof(S_Frag) 的元素,vecPushBack() 为我管理内存内容

当我尝试 printf() *ext, index, Q, fragmentIn.next 时,一切看起来都很好

如能指出错误,将不胜感激,谢谢

哦,顺便说一句,除了我的 student.c 文件,我不能改变任何东西

编辑:我还尝试使用

从片段中获取深度
printf("::%f\n", fragmentOut->depth);

输出中有一些 0,-0.00000 以及 -1523...25 零...0.00000 之类的数字

最佳答案

好吧,我刚刚弄明白了,当然是我犯了错误而不是仔细检查......还有另一个函数可以清除缓冲区,我认为它不会经常清除它们,但实际上它非常清除它们经常导致所有的困惑 :D

谢谢你的时间,解决了

关于c - 指针扰乱了我的价​​值观,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29900633/

相关文章:

char ** 和解引用指针

c - 为什么这个数组中的第一个元素不存储任何值?

c - C 中的宏生成宏?

c - 在不更改代码的情况下使用 gcc 的功能多版本控制是否可行?

c - while 循环的使用

c++ - 使用 OpenCV 在 C++ 中缩放图像,但不使用 pyrDown() 或 pyrUp() 函数

c - 在C语言中获得计算机决策速度的方法

c++ - R:指向c函数的指针

c++ - 双向链表的输出

c - 使用指针在递归函数中创建一个计数器?