Is this correct usage of pointers?

Started by
19 comments, last by Hollower 21 years, 5 months ago
As I was writing this program I suddenly felt unsure about pointers. I'm not yet in a position to test-build so I'm asking here if this is correct before I write a bunch of code which uses these. These structures will hold animation data. Each animation can move an arbitrary number of objects, and each object can have an arbitrary number of key frames.
    
struct ANIMDATA
{
    CString AnimName;
    int ObjectCount;

    OBJECTDATA* pObjects;
};

struct OBJECTDATA
{
    CString ObjectName;
    int FrameCount;
    bool bRoot;

    FRAMEDATA* pFrames;
};

struct FRAMEDATA
{
    D3DXQUATERNION Rotation;
    D3DXVECTOR3 Translation;
    float fTimeIndex;
};
  
Then after I have a ANIMDATA* pAnims assigned, I figured I'd do this within a loop as it reads from a file...
        
(pAnims+i)->pObjects = new OBJECTDATA[(pAnims+i)->ObjectCount];
  
...and likewise for pFrames. Yes, ObjectCount will have been assigned before this part. Is this the right way to use the -> operator and the right way to create an array of arbitrary size? [edited by - Hollower on November 20, 2002 12:36:28 AM]
Advertisement
If your purpose of pointers are just for having a ''dynamic'' array, I would suggest using std::vector (or alike) to reduce fever and stars in your head. eg.


  struct ANIMDATA{      CString AnimName;      int ObjectCount;      std::vector<OBJECTDATA> vObjects;};struct OBJECTDATA{      CString ObjectName;      int FrameCount;      bool bRoot;      std::vector<FRAMEDATA> vFrames;};struct FRAMEDATA{      D3DXQUATERNION Rotation;      D3DXVECTOR3 Translation;      float fTimeIndex;};  std::vector<ANIMDATA> vAnims;...vAnims[i].vObjects.resize(vAnims[i].ObjectCount);// orvAnims[i].vObjects.reserve(vAnims[i].ObjectCount);  

... and remember to look up the doc for more samples and detail explanations.
"after many years of singularity, i'm still searching on the event horizon"
yes, you are using them correctly.

the code you provided will not compile though.
you are declaring structs within each before they are defined.
so, you need to rearrange the structs.
FRAMEDATA first.
OBJECTDATA
ANIMDATA last.

also you need to declare them as such..

struct FRAMEDATA {
} FRAMEDATA;

if you do not declare them this way every time you declare
a "new" FRAMEDATA you _must_ put "struct" in front of it.

so in your current code, youd need to do...

struct OBJECTDATA {
.
.
struct FRAMEDATA *pFrames;
};

for it to compile.

yes, you can use the std::vector that DerekSaw suggested,
but if i were you, id learn how to use pointers properly
from the beginning instead of having to rely on something
someone else wrote.
I thought that structs were pretty much of no use. Isn''t a class just a more advanced and organized structure. So why use structs?????
Well there are some reasons why a structure may be prefferable to a class.

One unique example is online gaming. Structures have a lot less overhead then classes, so they are more beneficial for transfer over the internet. Less overhead means they will be smaller in size. However, classes do have many more advantages over structures most of the time. It just depends on what you need, and your personal preference.

C++ structs and classes are virtually identical. The only difference is that the members are public by default in a struct and private by default in a class. There is no additional overhead in using classes instead of structs, and a class is not more advanced.
Thanks Derek I will look into vectors. I've seen them mentioned but never anything in-depth. I'll search but if anyone knows a good vector tutorial link that would be nice.

Thanks Anon, I didn't even realize that. My limited knowledge is becoming apparent with this little animation experiment. I've never tried to build a "tree" structure before. As for the rest of the comments, maybe it's a bad habit but I tend to use struct for simple data, and class for more "functional" things. I know they are identical except for the public/private thing.


[edited by - Hollower on November 21, 2002 11:17:37 PM]
quote:if you do not declare them this way every time you declare
a "new" FRAMEDATA you _must_ put "struct" in front of it.

Forward declaration might do the trick.
quote:So why use structs?????

struct still, by default, publicly accepted.
quote:Structures have a lot less overhead then classes, so they are more beneficial for transfer over the internet

Who say so? Prove it technically, and have the whole world believe it.
"after many years of singularity, i'm still searching on the event horizon"
While we''re on the topic of structs, I sometimes see people do this:

typedef struct SOMETHING {
...

Does that make something different happen?
A std::vector simple sample:

  #include <iostream>   #include <vector>    int main(){  std::vector<int> vNumber;  // add to the tail of the vector  vNumber.push_back(7);  vNumber.push_back(5);  vNumber.push_back(11);  // ''read'' access  cout << vNumber[0] << endl; // output: 7  cout << vNumber[1] << endl; // output: 5  cout << vNumber[2] << endl; // output: 11  cout << vNumber.size() << endl; // output: 3  // ''write'' access  vNumber[0] = 13;  cout << vNumber[0] << endl; // output: 13  // clear your vector  vNumber.clear();  cout << vNumber.size() << endl; // output: 0  return 0;}[/soure]  
"after many years of singularity, i'm still searching on the event horizon"

This topic is closed to new replies.

Advertisement