Advertisement

Ahhh problems again :(

Started by May 16, 2004 12:49 PM
3 comments, last by DMINATOR 20 years, 6 months ago
K now i come to this point I have following structures : typedef struct tagVERTEX { float x, y, z; float u, v; } VERTEX; typedef struct tagTRIANGLE { VERTEX vertex[3]; } TRIANGLE; typedef struct tagSECTOR { int stexture; int numtriangles; TRIANGLE *triangle; } SECTOR; and how can i allocate an array of SECTORS ? SECTOR *objects; objects = (SECTOR*)malloc(objectnum * (sizeof(SECTOR)) ); Some how this doesn''t work The vertex seems'' not to be initialized proparly , i can''t seem to find any way to initialize it plz help.
You initialize sectors properly, however you haven''t initialized the triangle pointer and therefor it is invalid.
Advertisement
quote: Original post by Tree Penguin
You initialize sectors properly, however you haven''t initialized the triangle pointer and therefor it is invalid.


K i have changed the structures to :

typedef struct tagVERTEX
{
float x, y, z;
float u, v;

} VERTEX;

typedef struct tagTRIANGLE
{
VERTEX *vertex;

} TRIANGLE;

typedef struct tagSECTOR
{
int stexture;

int numtriangles;
TRIANGLE *triangle;

} SECTOR;

And now i initialize it with :

objects = (SECTOR*)malloc(objectnum * (sizeof(SECTOR)) );

objects[0].triangle = (TRIANGLE*)malloc(objects[0].numtriangles * (sizeof(TRIANGLE)) );

objects[0].triangle[0].vertex = (VERTEX*)malloc(3 * (sizeof(VERTEX)) );

/* Error Unhandled exception again */


Did initialize objects[0].numtriangles before using it to malloc object [0].triangle?

When you malloc something, the memory allocated contains arbitrary data. If using the compiler in debug mode, it might set all allocated memory to 0.

Actually, you should do something like this:
int nSize;nSize = objectnum * sizeof(SECTOR);objects = (SECTOR*)malloc(nSize));if (!objects)   return; //errormemset (objects, 0, nSize); //clear everythingobjects [0].numtriangles = some_useful_value; //here's your problem!      nSize = objects[0].numtriangles * sizeof(TRIANGLE);objects[0].triangle = (TRIANGLE*)malloc(nSize));if (!objects [0].triangle) {   free (objects);   objects = NULL;   return; //error   }memset (objects [0].triangle, 0, nSize); //clear again - this is another buffernSize = 3 *sizeof(VERTEX);objects[0].triangle[0].vertex = (VERTEX*)malloc(nSize));if (!objects[0].triangle[0].vertex) {   free (objects [0].triangle);   free (objects);   objects = NULL;   return; //error   }memset (objects [0].triangle [0].vertex, 0, nSize); //clear    


[edited by - karx11erx on May 16, 2004 3:51:02 PM]
_________karx11erxVisit my Descent site or see my case mod.
quote: Original post by karx11erx
Did initialize objects[0].numtriangles before using it to malloc object [0].triangle?

When you malloc something, the memory allocated contains arbitrary data. If using the compiler in debug mode, it might set all allocated memory to 0.

Actually, you should do something like this:
int nSize;nSize = objectnum * sizeof(SECTOR);objects = (SECTOR*)malloc(nSize));if (!objects)   return; //errormemset (objects, 0, nSize); //clear everythingobjects [0].numtriangles = some_useful_value; //here''s your problem!          nSize = objects[0].numtriangles * sizeof(TRIANGLE);objects[0].triangle = (TRIANGLE*)malloc(nSize));if (!objects [0].triangle) {   free (objects);   objects = NULL;   return; //error   }memset (objects [0].triangle, 0, nSize); //clear again - this is another buffernSize = 3 *sizeof(VERTEX);objects[0].triangle[0].vertex = (VERTEX*)malloc(nSize));if (!objects[0].triangle[0].vertex) {   free (objects [0].triangle);   free (objects);   objects = NULL;   return; //error   }memset (objects [0].triangle [0].vertex, 0, nSize); //clear     


[edited by - karx11erx on May 16, 2004 3:51:02 PM]


K thank you , i now understand my problem , it is all clear now .

This topic is closed to new replies.

Advertisement