|
-
December 28th, 2005, 09:32 AM
#1
Pointer confusion
ok, I am trying to extract some data and the size of that data out of a structure. the structure is inside a class.
Class is called MODEL_3DS and I have created an object instance of the class called m
the structure is as follows
Code:
struct Object {
float *Vertexes; // The array of vertices
float *TexCoords; // The array of texture coordinates for the vertices
int numVerts; // The number of vertices
int numTexCoords; // The number of vertices
};
Object *Objects; // The array of objects in the model
I can get to the data in for normal items using m.Objects[i].numTexCoords which give me the number of texture coordinates
Question is when since vertexes is just a pointer it just contains a 4 byte address to the data. How do I get to the data and the size of the data..
I have tried:
sizeof(&m.objects[0].Vertexes) to no avail.
thanks for your help...andrew
-
December 28th, 2005, 09:46 AM
#2
Re: Pointer confusion
Code:
struct Object {
float *Vertexes; // The array of vertices
float *TexCoords;// The array of texture coordinates for the vertices
int numVerts; // The number of vertices
int numTexCoords;// The number of vertices
};
Object obj={0};
obj.numVerts = 128;
obj.numTexCoords = 128;
obj.Vertexes = new float[obj.numVerts];
obj.TexCoords = new float[obj.numTexCoords];
int sizeOfVertexes = sizeof(float)*obj.numTexCoords;
int sizeOfTexCoords = sizeof(float)*obj.numVerts;
int sizeOfAllStruct = sizeOfVertexes+sizeOfTexCoords+sizeof(int)*2;
delete []obj.Vertexes;
delete []obj.TexCoords;
Cheers
-
December 28th, 2005, 10:38 AM
#3
Re: Pointer confusion
Ok taking what you posted as a guide I think I got it to work using the following
Code:
float * getTexCoords;
float * getVertexes;
getVertexes = new float[m.Objects[0].numVerts];
getTexCoords = new float[m.Objects[0].numTexCoords];
int sizeOfVertexes = sizeof(float)*m.Objects[0].numTexCoords;
int sizeOfTexCoords = sizeof(float)*m.Objects[0].numVerts;
I think this is working but if I screwed it up somehow let me know.
Thanks very much for your help....All I needed was a prompt in the right direction....hehe...
-
December 28th, 2005, 11:15 AM
#4
Re: Pointer confusion
Now I have to figure out how to get to the data itself.
I am not sure that my line getVetexes is actually getting the vertes information because when I output it I am not getting a set of vertexes.
Am I doing something wrong?
Last edited by Mogulbasher; December 28th, 2005 at 11:36 AM.
-
December 28th, 2005, 11:24 AM
#5
Re: Pointer confusion
 Originally Posted by Mogulbasher
I think this is working but if I screwed it up somehow let me know.
The potential problem with your code is that your Object class must now have a user-defined copy constructor, assignment operator, and destructor for the proper memory management to occur. If you leave any of them out, usage of your class in anything but a trivial program will more than likely lead to memory leaks or bugs.
The proper way to do this in C++ is to use a container class. For straight (ANSI) C++, the container class is std::vector. For MFC, the container class that you can use is CArray (but you can still use the vector in an MFC application).
Here is your Object class, but written much more safely using std::vector:
Code:
#include <vector>
struct Object
{
std::vector<float> Vertexes; // The array of vertices
std::vector<float>TexCoords; // The array of texture coordinates for the vertices
};
Note that there is no need for those other two int's, since vector knows its size by invoking the size() member function.
Now when you need to initialize the vector, you can do it in the constructor of Object, or if not the constructor, just call "resize" with the right number of floats for each vector.
Code:
int main()
{
Object o1;
o1.Vertexes.resize( 10 ); // 10 vertices
o1.TexCoords.resize( 10 ); // 10 vertices
Object o2 = o1;
}
The resize() takes any integer expression. Also, the last line in the sample shows a copy construction taking place. If your original class didn't have a user-defined copy constructor, the last line will cause a memory leak, and the ending brace of main() would invoke the destructor, causing more memory leaks, or a double deletion error (if you did just code only a destructor), since you used pointers, and pointers do not know how to copy the data being pointed to.
In the example above, since vector knows how to copy itself, the vectors will copy the floats from o1 to o2.
Basically, there is little, if any need to do "new T[]", "delete []" in a C++ program since there are container classes that encapsulate this whole thing (and most of all, they handle the memory management for you).
Regards,
Paul McKenzie
-
December 28th, 2005, 11:32 AM
#6
Re: Pointer confusion
Also, to add -- the vector container (and CArray) stores the data in contiguous memory, just like an array. So if you need to call a function that takes a pointer to a buffer of floats, you can use vector/CArray and pass the internal buffer to the function using various techniques.
Also, operator [] has been overloaded to make any operations on vector or CArray work just like an array.
So there aren't really any major coding changes that would have to be made if you switched from pointers or arrays to container classes.
Regards,
Paul McKenzie
-
December 28th, 2005, 11:47 AM
#7
Re: Pointer confusion
I only started C++ programming about 6 months ago in my spare time and so I must confess this is getting a little bit beyond me. Which probably explains why my code is a little inefficient.
I sort of understand what you are suggesting, I am using MFC, and I will give it a go once I have at least got the program to do what I want it to do. This is not a commercial venture or application just a small app to convert a 3DS format to a proprietary format to help me learn. Once working this will be great next step as I try to make it better and more efficient.
I do have a constructor and destructor built in
I do get an out of memory error when trying to load a large 3DS object
But as a beginner I am trying to move forward one small step at a time...hehe...
thanks for the advice...Andrew
Oh and if anyone knows why getVertexData does not give me the data that would be great.
Last edited by Mogulbasher; December 28th, 2005 at 11:52 AM.
-
December 28th, 2005, 12:04 PM
#8
Re: Pointer confusion
Ooh, 3dSMax. Not the easiest format to work with.
We might be able to help you more if you show us the getVertexData function. Hard to debug a function without having access to its code
Errare humanum est, ergo non sum humanus.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|