473,791 Members | 3,074 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(&myObjec t, 0, sizeof(object3D ));
g_world.numObje cts ++;

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

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

g_world.m_objLi st.push_back(my Object);

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 1259

"berthelot samuel" <sa************ **@voila.fr> wrote in message
news:ca******** *************** ***@posting.goo gle.com...
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(&myObjec t, 0, sizeof(object3D ));
g_world.numObje cts ++;

myObject.vcnt = 8;
myObject.vertic es = new vertex[myObject.vcnt];
myObject.vertic es = 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.vertic es = new vertex[myObject.vcnt];
for (int i = 0; i < myObject.vcnt; ++i)
myObject.vertic es[i] = vertices[i];

myObject.fcnt = 10;
myObject.faces = new face[myObject.fcnt];
myObject.faces = faces;
Some problem again.

g_world.m_objLi st.push_back(my Object);


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.0 406170438.14bf7 976
@posting.google .com in comp.lang.c++:
myObject.vertic es = new vertex[myObject.vcnt];
myObject.vertic es = 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.goo gle.com...
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
3672
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 much, not even with swap) before throwing an exception. When the program tries to fill in the allocated memory with data, it is killed after using up the available memory. Does anyone know why the exception is not being thrown until well after...
9
2354
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 not necessary give it back to the OS. It seems that .NET win app will only return memory to the OS when the OS is asking for it. But!!! When the OS is asking for it is usually too late, tons of swapping and slow performance.
6
8214
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 allocation to create object of that class ? 2. If it says "dynamic memory allocation", is it mean the following code : DestinationAddress* dest = new DestinationAddress(); // code 1
13
5744
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 hoping the clc crowd had some ideas for making it even simpler! In-lined below the full program (132 lines). It's a circular singular linked list of "cells" inspired by the one in Plauger's text
8
4756
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 least)...the subject is "4 GB hard constraint on a Solaris 8 server". I figured I'd post over here because we aren't getting anywhere too fast over there, and I think it is a code issue. We have a piece of software that we purchased. It is a...
74
4704
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 seems to be setting a NULL pointer to the end of the list, and here we know that the allocated memory has been exhausted. All good. When this is a pointer to another type, say int, I could have a variable that records how much memory is being...
62
17860
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(); img.src = ; Then I use the image a few more times by adding it into an Array object:
66
3645
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 (function(socket, args) == -1) { perror("function"); exit(EXIT_FAILURE); } I feel that the ifs destroy the readability of my code. Would it be
26
3065
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 read some of the items from the bottom up of the buffer, and some from the top down, moving the bottom items back to the new re-allocated bottom on every file read. Then when I've read all four files, I sort the top and bottom items separately...
10
4430
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 the allocation is impossible. Sworna vidhya
0
9669
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9517
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10428
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10156
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9997
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9030
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6776
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5435
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5559
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.