Jul 26, 2009

Anything wrong in glShaderSourceARB ?

It cost me many hours to find what was wrong with glShaderSourceARB. When I wrote this :

char v[128] =
"void main(void)\n"
"{\n"
" gl_Position = ftransform();\n"
"}\n";

glShaderSourceARB(shaderVtx, 1, (const GLcharARB**)&v, 0);

My application always crashed in "strlen". But if I change the code to :

char v[128] =
"void main(void)\n"
"{\n"
" gl_Position = ftransform();\n"
"}\n";

const GLcharARB* ppV[1] = {(GLcharARB*)v};

glShaderSourceARB(shaderVtx, 1, ppV, 0);

Everything is fine. Do I forget how to write C ? (Although I insert them in objective-c.) It looks like due to compiler setting since the first one works in sample of xcode.

20090802
Ok, nothing wrong except my careless style. This is a very basic pointer usage problem. "v" in the code does mean the start address of a string, but not a pointer to a string (althought the type is a pointer). That means "&v" is not in the memory. But why the sample works ? Because the sample pass "v" to another function which would generate a new local pointer to v, so we can find this pointer in memory. Luckily, no one would read this article. XD

No comments: