Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Hollower

Is this correct usage of pointers?

This topic is 5691 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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);
// or

vAnims[i].vObjects.reserve(vAnims[i].ObjectCount);

... and remember to look up the doc for more samples and detail explanations.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
While we''re on the topic of structs, I sometimes see people do this:

typedef struct SOMETHING {
...

Does that make something different happen?

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!