C++ malloc typedef struct which is in a typedef struct already

5 pts.
Microsoft Visual Studio
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

Answer Wiki

Thanks. We'll let you know when a new response is added.

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:

since it is a pointer it should use the statement as below to access the members instead of (.).

Hope this might be helping out to solve your issue.


Discuss This Question: 1  Reply

There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.
  • carlosdl
    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 ?
    85,885 pointsBadges:

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

Thanks! We'll email you when relevant content is added and updated.


Share this item with your network: