473,383 Members | 1,929 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

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

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
Jul 22 '05 #1
5 1240

"berthelot samuel" <sa**************@voila.fr> 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<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[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<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
Jul 22 '05 #2
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
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

"berthelot samuel" <sa**************@voila.fr> 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

Użytkownik "John Harrison" <jo*************@hotmail.com> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Alan Gifford | last post by:
I wrote a program to make sure that new would throw a bad_alloc exception if more memory was requested than was available. On my system, new allocates up to 2931 MBs of memory (I don't have that...
9
by: Mike P | last post by:
I know everything about reference counting and making sure you don't have large objects lying around. I have also profiled my app with multiple tools. I know about the fact GC collects memory but...
6
by: chris | last post by:
Hi all, I need to know, what is the difference between dynamic memory allocation, and stack allocation ? 1. If I have a class named DestinationAddress, when should I use dynamic memory...
13
by: Michael B Allen | last post by:
Hi, I've tried to write the *simplest* memory allocator possible. I think it would be useful in many cases such as allocating memory on stack as a poor man's garbage collection perhaps. I was...
8
by: clsmyth | last post by:
Folks, Hi, I have never posted to a language group before so please excuse me if this is inappropriate. I have posted this to comp.unix.solaris (well, I am one of the folks on the thread at...
74
by: ballpointpenthief | last post by:
If I have malloc()'ed a pointer and want to read from it as if it were an array, I need to know that I won't be reading past the last index. If this is a pointer to a pointer, a common technique...
62
by: ivan.leben | last post by:
How can I really delete a preloaded image from memory/disk cache? Let's say I preload an image by creating an Image object and setting its src attribute to desired URL: var img = new Image();...
66
by: Johan Tibell | last post by:
I've written a piece of code that uses sockets a lot (I know that sockets aren't portable C, this is not a question about sockets per se). Much of my code ended up looking like this: if...
26
by: Bill Reid | last post by:
Bear with me, as I am not a "professional" programmer, but I was working on part of program that reads parts of four text files into a buffer which I re-allocate the size as I read each file. I...
10
by: swornavidhya.mahadevan | last post by:
Which allocation (Static / Dynamic) is suitable for the situation when we are trying to allocate for a overloaded memory when the memory is full and no space to allocate. What will happen if both...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.