ionas February 2016

Reading pointer data

i'm using a library which has an OpenGL like syntax. When using a function to get data to a pointer, it sometimes brings in the first cells irrelevant numbers.

int *rIndices = (int*)malloc(icount * 500* sizeof(int));
giGetIndexedMesh(&vcount, &icount, rIndices);
for (int j = 0; j < icount; j += 3)
{
    printf("%d %d %d\n", rIndices[j], rIndices[j + 1], rIndices[j + 2]);
}

And the out put is:

-1107141503 1047065916 1058222111
- 1118597697 -1083120598 1041096918
-1118541674 -1082575688 1040872831
-1129197583 -1082576454 1041095778
170 190 135
100 174 172
116 175 184
176 191 178
177 176 178

This doesn't happen always. Has anyone encountered a similar issue?

PS the numbers should be like the 5th row and after. thanks in advance.

Answers


Martin Zabel February 2016

After taking a quick look at the documentation, you should call the function giGetIndexedMesh twice. First, to retrieve the required sizes (vcount and icount) by setting the last parameter to NULL. And the second time to retrieve the indices.

In your code snippet, you allocate the array using an old value of icount. The required value is determined later, but you must determine it before:

GIuint vcount, icount;
giGetIndexedMesh(&vcount, &icount, NULL);
GIunit *indices = malloc (...);
giGetIndexedMesh(&vcount, &icount, indices);

I didn't find enough information on the array layout. Thus, I didn't completed the malloc call. You should also check the return value of malloc an giGetIndexedMesh.

Post Status

Asked in February 2016
Viewed 1,509 times
Voted 13
Answered 1 times

Search




Leave an answer