   3Ds loading: texture coordinates - infinite loop
  Hi!I want to load .3Ds files. My code works fine, if I ignore texture coordinates (comment the case statement), and uncheck the "preserve MAX1s texture coordinates" in 3Ds MAX 5 or 9.But if I try to use text coords (case uncommented), the loader runs into an infinite loop.("preserve MAX1s texture coordinates" is checked)The code is stolen from somewhere than modified. I looked at many loader codes, all had the same structure for the texture loading part, so I have no idea what's wrong.void LoadMesh(const char *fnev,int *vert_num,int *poli_num,float *vertex,int *polygon){FILE *stream;unsigned short chnk_ID;unsigned int chnk_lng;unsigned char byte;int i;long filelength;float text_koord[200000];int text_num;stream = fopen(fnev,"rb");fseek(stream,0,SEEK_END);filelength = ftell(stream);fseek(stream,0,SEEK_SET);while( ftell(stream) < filelength ){fread(&chnk_ID,2,1,stream);fread(&chnk_lng,4,1,stream);switch( chnk_ID ){case 0x4d4d:break;case 0x3d3d:break;case 0x4000:dofread(&byte,1,1,stream);while( byte != '\0' );break;case 0x4100:break;case 0x4110:fread(vert_num,2,1,stream);for( i = 0; i < (*vert_num)*3; i++ ){fread(vertex,4,1,stream);vertex++;}break;case 0x4120: //quads, it works finefread(poli_num,2,1,stream);for( i = 0; i < (*poli_num)/2; i++ ){fread(polygon  ,2,1,stream);fread(polygon+1,2,1,stream);fread(polygon+2,2,1,stream);fseek(stream,4,SEEK_CUR);fread(polygon+3,2,1,stream);fseek(stream,4,SEEK_CUR);polygon += 4;}break;case 0x4140: //texture coordsfread(&text_num,2,1,stream);for( i = 0; i < text_num*2; i++ ){fread(&text_koord,4,1,stream);}break;default:fseek(stream,chnk_lng-6,SEEK_CUR);}}fclose(stream);}The model I'm trying to load has 300 vertices. I don't use the text coords, because I'm still debugging.Thanks!   
  You declared the number of texture coordinates as a signed integer (4 bytes), even though the number of texcoords is defined to be an unsigned short (2 bytes).  You read the number correctly, but only store it in the first two bytes of text_num.  This leaves the last two bytes untouched.  If text_num = 0 to start off, this is ok.  But say text_num is initialized to a large number (0x0FFFFFFF) (hypothetically).  Then when you do fread(&text_num,2,1,stream) for 300 vertices, text_num is set to 2C 01 FF 0F (which overwrites the first two bytes, but leaves the last two untouched).  But on an LSB machine,  2C 01 FF 0F = large number, so your loop goes for a long time.  Try looking at it in the debugger.The solution is to change text_num from int to unsigned short.   ;  Thank for the help!It was a noob mistake, that I didn't notice, since it worked with the other integers, because they pointed to global variables, so they were zeroed.It's funny how amateur faults I still make.Than an other question:How does the .3Ds store the values, after UW mapping, so the number of texture coords != number of vertices.Or maybe I should figure it out.Thanks again!   ;  figured out.Have to index the text coords, as the vertex coords.   ;  In both C and C++, you don't get very many guarantees about the exact size of the integral types. You could also have avoided the problem by simply initializing the variable to 0, which is probably a good idea anyway.   