5 pts.
 C++ malloc typedef struct which is in a typedef struct already
Ok so I've got these typedef structs:
typedef struct {
           float objectTransformX;
} Object;

typedef struct {

           float lookAtZ;
} SceneCamera;

typedef struct {
           Object *Object;
           SceneCamera Camera;

           bool Active;

} Frames;

And I've got this class:


class Scenario {

           private:
                      int currentFrame;
                      int fps;

                      bool allowOverwrite;

                      Frames *Frame;

                      .....


}

And I've got the constructor:
 Scenario::Scenario(int objectCount,int lengthInSeconds,bool allowOverwrite,int fps) {
           frameCount = lengthInSeconds*fps;
           Frame = (Frames*)malloc(sizeof(Frames)*frameCount);
           Frame->Object = (Object*)malloc(sizeof(Object)*objectCount);
           ....
           Frame[t].Camera.lookAtZ = 0;

           for (int i = 0;i < objectCount;i++) {

                      Frame[t].Active = false;
                                                         Frame[t].Object.objectTransformX = 0;



                      .....
           }
}

Why do I get an exception when I try to assign a value to objectTransformX... I did allocate memory didn't I?
I got an unhandled exception, which states that the memory is outside the program.
CXX0030 error in VC++.


Software/Hardware used:
Microsoft Visual Studio
ASKED: Dec 19, 2009  8:47 PM GMT
UPDATED: January 30, 2010  6:26:43 AM GMT
315 pts.

Answer Wiki:
Hi Mr.JammerLT,

From the code snippet I could see that the Object variable (member of the Frame Structure) is allocated with size as ObjectCount each having the size of object. So when accessing the member of Object structure either you have to use statements as below:
Frame[t].Object[i].objectTransformX=0;

since it is a pointer it should use the statement as below to access the members instead of (.).
Frame[t]->Object[i]->objectTransformX=0.


Hope this might be helping out to solve your issue.

Thanks,
-Balaji
Last Wiki Answer Submitted:  Jan 30, 2010  6:26 AM (GMT)  by  Techy001   315 pts.
To see other answers submitted to the Answer Wiki View Answer History.
Discuss This Question:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _




 

Does this code compile ?

Since *Object is a pointer, I think you should use this notation:

Frame[t].Object->objectTransformX = 0;

instead of:

Frame[t].Object.objectTransformX = 0;

As for the memory exception, you are allocating memory for objectCount Objects and frameCount Frames, but we don’t know what’s the value of ‘t’ (in fact, we don’t even see where you declare it) at the time you get the error.

Also, what is this code doing inside a for loop where you only increment ‘i’ (and ‘t’ remains unchanged) ?:

Frame[t].Active = false;
Frame[t].Object.objectTransformX = 0; 

On the other hand, why are you using C’s malloc function instead of C++’s new operator ?

 60,245 pts.