memory allocation problem (well... I guess so)

B

berthelot samuel

Hi,
I have a problem with structures that lose their content when I pass
them to a function.

Here are the structures

typedef struct VERTEX
{
int id; /* id of this vertex */
float x, y, z; /* vertex coordinates */
float u, v; /* texture coordinates */
float r, g, b; /* rgb colour of the vertex */
float normal[3]; /* x y z values of the normal */
}vertex;

typedef struct FACE
{
int vertices[3];/* list of vertices corresponding to this face */
CVector normal; /* x y z values of the normal */
}face;

typedef struct OBJECT3D
{
vertex *vertices; /* hold the vertices of the object3D */
face *faces; /* hold the faces of the object3D */
int vcnt; /* number of vertices of the object3D */
int fcnt; /* number of faces of the object3D */
CVector center; /* center of the object - used for bounding box
*/
float width; /* max width of the object -used for bounding box */
void ComputeBB(); /* compute the bounding box of the object */
}object3D;


typedef struct WORLD3D
{
int numObjects; /* number of objects in the world */
vector<object3D> m_objList; /* list of objects in the world */
}world3D;



And there I create my objects and I add them to a vector


object3D myObject;
memset(&myObject, 0, sizeof(object3D));
g_world.numObjects ++;

myObject.vcnt = 8;
myObject.vertices = new vertex[myObject.vcnt];
myObject.vertices = vertices;

myObject.fcnt = 10;
myObject.faces = new face[myObject.fcnt];
myObject.faces = faces;

g_world.m_objList.push_back(myObject);



I've checked with the debugger and g_world contains everything ok. The
problem is there:


m_octree->BuildRootNode(&g_world, m_city->GetNumPoly(),
m_octree->GetCenter(), m_octree->GetSize());

In BuildRootNode(...) g_world doesn't contain the value of vertices
and faces any more. I believe it's related to the way I pass it to the
function, but I'm not too sure about that.
Thanx for any help.
Sam
 
R

Rob Williscroft

berthelot samuel wrote in @posting.google.com in comp.lang.c++:
myObject.vertices = new vertex[myObject.vcnt];
myObject.vertices = vertices;
myObject.faces = new face[myObject.fcnt];
myObject.faces = faces;
In BuildRootNode(...) g_world doesn't contain the value of vertices
and faces any more. I believe it's related to the way I pass it to the
function, but I'm not too sure about that.
Thanx for any help.

If thats not enough then you haven't posted enough relevent code.

HTH.

Rob.
 
S

Sharad Kala

berthelot samuel said:
Hi,
I have a problem with structures that lose their content when I pass
them to a function.

What are CVector, g_world etc??
Post the minimal compilable code that demonstrates your problem. Then someone
can help you here.
 
J

John Harrison

berthelot samuel said:
Hi,
I have a problem with structures that lose their content when I pass
them to a function.

Here are the structures

typedef struct VERTEX
{
int id; /* id of this vertex */
float x, y, z; /* vertex coordinates */
float u, v; /* texture coordinates */
float r, g, b; /* rgb colour of the vertex */
float normal[3]; /* x y z values of the normal */
}vertex;

typedef struct FACE
{
int vertices[3];/* list of vertices corresponding to this face */
CVector normal; /* x y z values of the normal */
}face;

typedef struct OBJECT3D
{
vertex *vertices; /* hold the vertices of the object3D */
face *faces; /* hold the faces of the object3D */
int vcnt; /* number of vertices of the object3D */
int fcnt; /* number of faces of the object3D */
CVector center; /* center of the object - used for bounding box
*/
float width; /* max width of the object -used for bounding box */
void ComputeBB(); /* compute the bounding box of the object */
}object3D;


typedef struct WORLD3D
{
int numObjects; /* number of objects in the world */
vector<object3D> m_objList; /* list of objects in the world */
}world3D;



And there I create my objects and I add them to a vector


object3D myObject;
memset(&myObject, 0, sizeof(object3D));
g_world.numObjects ++;

myObject.vcnt = 8;
myObject.vertices = new vertex[myObject.vcnt];
myObject.vertices = vertices;

What is this? Why do you assign to vertices twice? What is the veritices
variable?

I think you probably meant something like this

myObject.vertices = new vertex[myObject.vcnt];
for (int i = 0; i < myObject.vcnt; ++i)
myObject.vertices = vertices;
myObject.fcnt = 10;
myObject.faces = new face[myObject.fcnt];
myObject.faces = faces;

Some problem again.
g_world.m_objList.push_back(myObject);

You've already used vectors elsewhere in your code, so why didn't you use
them for vertices and faces? As you've seen using pointers and dynamic
memory is tricky. Consider doing this instead

struct OBJECT3D
{
vector<vertex> vertices; /* hold the vertices of the object3D */
vector<face> faces; /* hold the faces of the object3D */
CVector center; /* center of the object - used for bounding box */
float width; /* max width of the object -used for bounding box */
void ComputeBB(); /* compute the bounding box of the object */
};

john
 
J

John Harrison

Also don't use CVector, why use two different vector classes in the same
program, isn't that just confusing? Stick to vector, it is by far the best
alternative.

john
 
M

Marcin Kalicinski

U¿ytkownik "John Harrison said:
Also don't use CVector, why use two different vector classes in the same
program, isn't that just confusing? Stick to vector, it is by far the best
alternative.

From the code, I think that OP CVector is a mathematical vector with 3 float
components, implementing some maths operations like dot product etc. So it
cannot be replaced with std::vector.

Anyway, I wonder why he hadn't used it for VERTEX position or normal.

Best regards,
Marcin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top