texture creating: what's wrong?

What I want to do is creating a texture from a array of byte. It’s called “tex”.
I have an array GLunit texture[1] declare before that to handle the texture.
Whats wrong with my code? It compiles but it crashes immediatly.

//filling the byte array with a lot of 128 bytes
//that I thing should make a gray texture
      int i;
      for (i=0; i <= 768-1;i++)tex[i]= 128;
      glGenTextures(1, &texture[0]);
      glBindTexture(GL_TEXTURE_2D, texture[0]);
      glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
      glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
      glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB, 256, 256, 0, GL_RGB, GL_UNSIGNED_BYTE, tex);

in my render methode I call again glBindtexture

… just modify these lines … it should work.

// define an array match the glTexImage2D declaration
unsigned char tex[256*256*3]; // RGB * height * width

// filling it
      for (i=0; i <256*256*3;i++)tex[i]= 128;

// the array and this must match !!
      glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB, 256, 256, 0, GL_RGB, GL_UNSIGNED_BYTE, tex);

By the way, you know that there are very useful tools called ‘debuggers’ that let you see precisely where a program crashes ?

Thanks a lot!
It works now, but shouldn’t it work what ever there is at tex? Something like a random map.

It can crash or not, it depends of many things. But the GL driver has to copy your texture in its own memory before returning to your program, as you may modify the array data afterwards.

The driver goes past your array, and start to read an other array of this program, or gets out of the allowed memory segments. Modern operating systems does not let programs read all around. So the kernel says ‘STOP !’ -> segmentation fault.