Re: memory allocation nightmare

From: berthelot samuel (samuel.berthelot_at_voila.fr)
Date: 04/23/04


Date: 23 Apr 2004 01:45:23 -0700

Hi,
You sound like you are taking the piss at me ! relax man.
When I said I had solved the problem mentioned above I was obviously
talking about the first thread in the list, read more carefully next
time!
I don't know if my structure are poorly designed, but what I know is
that neither gcc nor visual c++ ever complained about my structures.
Without the memory allocation problem, my program runs fine...

now to get back to my problem, here is the code and only the code that
screws up

Now I don't do any memory allocations before the ones that are
responsible for the crash, therefore there must be definitely
something wrong in this few lines. First my structures:

#define POINTS_ON_CURVE 50
#define PATCH_SIZE POINTS_ON_CURVE * POINTS_ON_CURVE

typedef struct _vertex {
        int id;
        vector3 pos;
        vector3 normal;
        float r, g, b;
        float u, v;
}vertex;

typedef struct _tri {
        int id;
        int vertices[3]; //vertices ids
        vector3 normal;
}tri;

typedef struct _cp {
        vector3** elmt; //16 control points of the patch
}cp;

typedef struct _patch {
        int id;
        vertex* vertices; //list of vertices of the patch
        tri* triList; //list of triangles of the patch
        cp* cpList; //list of control points
        int triCount; //number of triangles on the patch
        int vertexCount; //number of vertices on the patch
}patch;

typedef struct _object {
        int patchCount; //number of patches of the object
        patch *patch; //list of patches of the object
}object;

//an object made of surface patches
object* g_myObject;

Now the set of memory allocations:

        /*
        *Allocate memory for g_myObject and assign patches and control points
        */

        g_myObject = malloc(sizeof(object));
        g_myObject->patchCount = 4;
        g_myObject->patch = malloc(g_myObject->patchCount * sizeof(patch));
        
        for (i = 0; i < g_myObject->patchCount; i ++)
        {
        g_myObject->patch[i].vertices = malloc (PATCH_SIZE * sizeof(vertex));

        g_myObject->patch[i].triCount = (POINTS_ON_CURVE - 1) *
(POINTS_ON_CURVE - 1) * 2;

        g_myObject->patch[i].triList =
malloc(g_myObject->patch[i].triCount * sizeof(tri));
                
        g_myObject->patch[i].cpList = malloc(sizeof(cp));

        g_myObject->patch[i].cpList->elmt = (vector3**)malloc(4 *
sizeof(vector3));
                
        for (j = 0; j < 4; j ++)
          g_myObject->patch[i].cpList->elmt[j] = (vector3*)malloc(4 *
sizeof(vector3));
       
        }

In the first for loop, when i=2, after it executes the third line, the
value of < g_myObject->patchCount changed suddenly from 4 to 4802 (the
value of g_myObject->patch[i].triCount) for no reason !!!

Please if someone is really good at C programming could you help on
this, because I'm desperate :(
Thx
Sam

Barry Schwarz <schwarzb@deloz.net> wrote in message news:<c69p30$815$0@216.39.135.79>...
> On 22 Apr 2004 05:34:54 -0700, samuel.berthelot@voila.fr (berthelot
> samuel) wrote:
>
> >Hi,
> >I'm having serious trouble with memory allocation (again).
>
> You don't even get that far. Your first typedef is illegal. How can
> you execute if it doesn't compile?
>
> Additionally, why are you declaring a struct which consist of nothing
> more than a single union? A struct with a single member is usually a
> sign of poor design.
>
> >I've solved the problem mentioned above through changing my structures
> >(no more redundancies). However there's still something wrong as when
>
> Make up your mind. Either you have solved the memory allocation
> problem (the only one mentioned above) or you are having problems
> again.
>
> >I'm trying to allocate memory for more than one patch, things get
> >screwed up. Here are my new structures:
>
> Please post compilable code.
>
> >
> >#define POINTS_ON_CURVE 50
> >#define PATCH_SIZE POINTS_ON_CURVE * POINTS_ON_CURVE
> >
> >typedef struct _vector3 {
> > union
> > {
> > struct
> > {
> > float x, y, z;
> > };
> float v[3];
> > };
> >}vector3;
>
> snip 150 lines of incomplete and incomprehensible code.
>
>
> <<Remove the del for email>>



Relevant Pages