By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
 448,671 Members | 1,670 Online
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,671 IT Pros & Developers. It's quick & easy.

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

 P: n/a 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 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 Jul 22 '05 #1
Share this Question
5 Replies

 P: n/a "berthelot samuel" wrote in message news:ca**************************@posting.google.c om... 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 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[i] = vertices[i]; 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 vertices; /* hold the vertices of the object3D */ vector 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 Jul 22 '05 #2

 P: n/a berthelot samuel wrote in news:ca34f197.0406170438.14bf7976 @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. -- http://www.victim-prime.dsl.pipex.com/ Jul 22 '05 #3

 P: n/a 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 Jul 22 '05 #4

 P: n/a "berthelot samuel" wrote in message news:ca**************************@posting.google.c om... 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. Jul 22 '05 #5

 P: n/a Użytkownik "John Harrison" napisał w wiadomo¶ci news:2j*************@uni-berlin.de... 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 Jul 22 '05 #6

### This discussion thread is closed

Replies have been disabled for this discussion.